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" class="body">
<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 class="otp">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
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