Get text from span tag with Python and Selenium
To print the text Director
you can use either of the following Locator Strategies:
Using
css_selector
andget_attribute("innerHTML")
:print(driver.find_element(By.CSS_SELECTOR, "h2.jobTitle > span[title]").get_attribute("innerHTML"))
Using
xpath
and text attribute:print(driver.find_element(By.XPATH, "//h2[contains(@class, 'jobTitle')]/span[@title]").text)
Ideally you need to induce WebDriverWait for the visibility_of_element_located()
and you can use either of the following Locator Strategies:
Using
CSS_SELECTOR
and text attribute:print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "h2.jobTitle > span[title]"))).text)
Using
XPATH
andget_attribute("innerHTML")
:print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h2[contains(@class, 'jobTitle')]/span[@title]"))).get_attribute("innerHTML"))
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
You can find a relevant discussion in How to retrieve the text of a WebElement using Selenium - Python
References
Link to useful documentation:
get_attribute()
methodGets the given attribute or property of the element.
text
attribute returnsThe text of the element.
- Difference between text and innerHTML using Selenium
Selenium WebDriver -- get <span> text
As the element is within a <span>
tag, to extract the text $726.35 you can use either of the following solution:
cssSelector:
driver.findElement(By.cssSelector("span.calculateur-resultats-total#paiement-resultats")).getText();
xpath:
driver.findElement(By.xpath("//span[@class='calculateur-resultats-total' and @id='paiement-resultats']")).getText();
Update
As the results were unstable you can induce WebDriverWait for the visibilityOfElementLocated and you can use either of the following solutions:
cssSelector:
String myText = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("table[aria-label='User']"))).getAttribute("innerHTML");
xpath:
String myText = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//table[contains(@style,'vertbar')]"))).getAttribute("innerHTML");
Extract text from span element using selenium in python
To get the Mobile-Number
you can take the reference of the text Mobile
and find the next sibling.
Induce WebDriverWait
() and wait for visibility_of_element_located
() and following xpath.
print(WebDriverWait(browser,10).until(EC.visibility_of_element_located((By.XPATH,"//td[.//div[contains(.,'Mobile')]]/following-sibling::td[1]/div"))).text)
You need to import following libraries.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Selenium Python extract text between Span
You need to call the .text
method which is present in the Selenium Python binding.
.text
is present for web element
authors = driver.find_element_by_xpath("//div[@class='author-info hidden-md']/span/a/span").text
print(authors)
or
authors = driver.find_element_by_xpath("//a[contains(@href,'/author/Margareta-Osborn')]").get_attribute('innerHTML')
print(authors)
Update 1 :
driver.maximize_window()
wait = WebDriverWait(driver, 30)
driver.get("https://www.bookdepository.com/Rose-River-Margareta-Osborn/9781925324402")
authors = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.author-info.hidden-md span[itemprop='author'] span"))).text
print(authors)
How to get text which is inside the span tag using selenium webdriver?
Problem:
As far as I understand, departmentCategoryContent
is a list, not a single WebElement
, then it doesn't have the find_elements_by_tag_name()
method.
Solution:
you can choose 1 of 2 ways below:
You need for-each of list
departmentCategoryContent
first, thenfind_elements_by_tag_name()
.Save time with one single statement, using
find_elements_by_css_selector()
:
departmentCategory = driver.find_elements_by_css_selector('.a-spacing-micro.apb-browse-refinements-indent-2 .a-list-item span')
[ print(x.text) for x in departmentCategory ]
Test on devtool:
Explanation:
Your locator .a-list-item span
will return all the span
tag belong to the div
that has class .a-list-time
. There are 88 items containing the unwanted tags.
So, you need to add more specific locator to separate the other div. In this case, I use some more classes. .a-spacing-micro.apb-browse-refinements-indent-2
Related Topics
How to Use Variables in One Method into Another Method
Find Difference Between Two Strings
Prevent Multiple Login Using the Same User Name and Password
Java Coding Converted to Pseudo Code
Why Am I Getting the Error "The Method Is Undefined for the Type"
Maven Build Cannot Find Symbol When Accessing Project Lombok Annotated Methods,
Checking If a Document Exists in a Firestore Collection
Is There a Simpler Way to Check Multiple Values Against One Value in an If-Statement
Tomcat Is Running But 8080 Port Is Not Responding
How to Remove Line Breaks from a File in Java
Jparepository Findall() Returns Empty Result
Java Spring: Best Way to Convert a File to a Multipartfile
Regular Expression for Uppercase Letters in a String
Could Not Extract Resultset in Hibernate
How to Parse Json Boolean Value
In Java, How to Determine If a Char Array Contains a Particular Character