How to get text with Selenium WebDriver in Python
You want just .text
.
You can then verify it after you've got it, don't attempt to pass in what you expect it should have.
Get Text from an Element with Selenium (Python)
Does this work ?
wait = WebDriverWait(driver, 10)
desired_text = wait.until(EC.visibility_of_element_located((By.XPATH, "//p[contains(@class, 'sc-')]"))).text
print(desired_text)
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
How to get text from a html using Selenium and Python which has two elements with the same classname where I need to extract both
As per the HTML:
<div class='mesage-in'> cool text here </div>
<div class='mesage-in'> bad text here </div>
The following line line of code:
texto = navegador.find_element_by_class_name('message-in').text
will always identify the first matching element, extract the text and assign it to texto
. So when you try to print texto
, the text of the very first element i.e. cool text here is printed.
Solution
You can get all elements with same classname i.e. mesage-in
and put on a list as follows:
from selenium.webdriver.common.by import By
texto = navegador.find_elements(By.CLASS_NAME, 'message-in')
Now you can print the desired texts with respect to their index as follows:
To print cool text here:
print(texto[0].text) # prints-> cool text here
To print bad text here:
print(texto[1].text) # prints-> bad text here
Outro
You can also crate a list of the texts using List Comprehension and print them as follows:
texto = [my_elem.text for my_elem in driver.find_elements(By.CLASS_NAME, "message-in")]
print(texto[0]) # prints-> cool text here
print(texto[1]) # prints-> bad text here
How to extract the text from the webelements using Selenium and Python
This works to an extent:
driver.get("https://www.monster.com/jobs/search?q=Python-Developer&where=Las+Vegas%2C+NV&page=1")
WebDriverWait(driver, 30).until(EC.visibility_of_all_elements_located((By.XPATH, "//*[@data-test-id = 'svx-job-title']")))
jobs = driver.find_elements(By.XPATH, "//div[contains(@class, 'job-cardstyle__JobCardHeader')]")
all_jobs = [job.text for job in jobs]
print(all_jobs)
WebdriverWait imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Output:
['Software engineer III\nRandstad USA\nLas Vegas, NV', 'C\nPython Developer\nconfidential\n$55 - $65 / Per Hour', 'C\nSenior Software Engineer\nCox Communications Inc\nLas Vegas, NV', 'Mission Systems Engineer\nDCS Corporation\nLas Vegas, NV', 'G\nSoftware Engineer - 914\nGCR Technical Staffing\nHenderson, NV', 'Z\nNetSuite Developer\nZone & Company Software Consulting\nLas Vegas, NV', 'IT Project Engineer\nRauland Florida by Ametek, Inc.\nSunrise, NV', 'A\nWeb Developer\nArdor Global', 'Senior Software Engineer – Node\nMeridian Technology Group Inc.']
Process finished with exit code 0
You may split the list with \n
delimiter for further usage. Also, seems like this site loads the cards dynamically, i.e., as you scroll down, new cards load up, so you may not get all the cards in one instance.
How to get text, using Selenium Python XPATH
You want just .text
So, use this
elem = driver.find_element(By.XPATH,'//*[@id="t3_9kxrv6"]/div/div/div[3]/span/h2').text
Python and how to get text from Selenium element WebElement object?
Once you locate the element you can use the text property.
Example:
for element in self.driver.find_elements_by_tag_name('img'):
print element.text
print element.tag_name
print element.parent
print element.location
print element.size
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
Related Topics
What Are Logits? Differencebetween Softmax and Softmax_Cross_Entropy_With_Logits
How to Read Text from the Clipboard
Why Doesn't Pygame Draw in the Window Before the Delay or Sleep
Django. Override Save for Model
Saving a Numpy Array as an Image
What Is the Intended Use of the Optional "Else" Clause of the "Try" Statement in Python
Pandas: Adding New Column to Dataframe Which Is a Copy of the Index Column
Activate a Virtualenv with a Python Script
Shared-Memory Objects in Multiprocessing
How to Convert String to Datetime Format in Pandas Python
Append Multiple Values for One Key in a Dictionary
Sort() and Reverse() Functions Do Not Work
How to Draw Vertical Lines on a Given Plot
Stop Reading Process Output in Python Without Hang
Saving and Loading Multiple Objects in Pickle File
Comparing Two Numpy Arrays for Equality, Element-Wise