I'm trying to get td values of td, but unfortunately it's not working, may be there is something wrong
My html looks like
<table align="center" id="container" >
<tbody>
<tr>
<td>
<table cellspacing="0" id="content" cellpadding="0">
<tbody>
<tr>
<td id="header">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td id="logos">
<img id="laitho" src="" > </td>
<td valign="top" align="right" id="title"><p>test message</p></td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td id="visitor_id">
<p>visitor counter</p>
<p >842896</p>
</td>
</tr>
<tr>
<td id="pep_id">
<p>test message</p>
</td>
</tr>
<tr>
<td id="closing">
<p>Thank you!</p>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
I want to get to the value 842896 in python using selenium
driver.find_elements_by_xpath("//tr/td[contains(.otp)]").text
CodePudding user response:
You are using find_elements_by_xpath
, which will return a list of web element in Selenium python.
Since you are looking for only one web element I'd say to use find_element
With Css:
print(driver.find_element(By.CSS_SELECTOR, "table#content td#visitor_id p.otp").text)
With xpath
print(driver.find_element(By.XPATH, "//table[@id='content']//descendant::td[@id='visitor_id']//p[@class='tp']").text)
A better way would be to use explicit wait:
With Css:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "table#content td#visitor_id p.otp"))).text)
or With xpath
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//table[@id='content']//descendant::td[@id='visitor_id']//p[@class='tp']"))).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
CodePudding user response:
To print the text 842896 you can use either of the following locator strategies:
Using css_selector and
get_attribute("innerHTML")
:print(driver.find_element(By.CSS_SELECTOR, "table#content td#visitor_id p.otp").get_attribute("innerHTML"))
Using xpath and text attribute:
print(driver.find_element(By.XPATH, "//table[@id='content']//td[@id='visitor_id']//p[@class='otp']").text)
To extract the text 842896 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, "table#content td#visitor_id p.otp"))).text)
Using XPATH and
get_attribute("innerHTML")
:print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//table[@id='content']//td[@id='visitor_id']//p[@class='otp']"))).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