如何使用Selenium从table in Python的tr的td的p标签中获取文本
How to get the text from the p tag of td of tr of table in Python using Selenium
我正在尝试获取 td 的 td 值,但不幸的是它不起作用,可能有问题
我的html长得像
<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>
我想使用 selenium
获得 python 中的值 842896
driver.find_elements_by_xpath("//tr/td[contains(.otp)]").text
您正在使用 find_elements_by_xpath
,它将 return Selenium 中的网络元素列表 python。
由于您只寻找一个网络元素,我建议您使用 find_element
与Css:
print(driver.find_element(By.CSS_SELECTOR, "table#content td#visitor_id p.otp").text)
用xpath
print(driver.find_element(By.XPATH, "//table[@id='content']//descendant::td[@id='visitor_id']//p[@class='tp']").text)
更好的方法是使用显式等待:
与Css:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "table#content td#visitor_id p.otp"))).text)
或使用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)
进口:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
要打印文本 842896 您可以使用以下任一方法 :
使用css_selector和get_attribute("innerHTML")
:
print(driver.find_element(By.CSS_SELECTOR, "table#content td#visitor_id p.otp").get_attribute("innerHTML"))
使用 xpath 和 text 属性:
print(driver.find_element(By.XPATH, "//table[@id='content']//td[@id='visitor_id']//p[@class='otp']").text)
要提取文本 842896 理想情况下,您需要引入 WebDriverWait for the and you can use either of the following :
使用 CSS_SELECTOR 和 text 属性:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "table#content td#visitor_id p.otp"))).text)
使用 XPATH 和 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"))
注意:您必须添加以下导入:
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
参考资料
Link 到有用的文档:
get_attribute()
方法Gets the given attribute or property of the element.
text
属性returnsThe text of the element.
- Difference between text and innerHTML using Selenium
我正在尝试获取 td 的 td 值,但不幸的是它不起作用,可能有问题
我的html长得像
<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>
我想使用 selenium
获得 python 中的值 842896driver.find_elements_by_xpath("//tr/td[contains(.otp)]").text
您正在使用 find_elements_by_xpath
,它将 return Selenium 中的网络元素列表 python。
由于您只寻找一个网络元素,我建议您使用 find_element
与Css:
print(driver.find_element(By.CSS_SELECTOR, "table#content td#visitor_id p.otp").text)
用xpath
print(driver.find_element(By.XPATH, "//table[@id='content']//descendant::td[@id='visitor_id']//p[@class='tp']").text)
更好的方法是使用显式等待:
与Css:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "table#content td#visitor_id p.otp"))).text)
或使用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)
进口:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
要打印文本 842896 您可以使用以下任一方法
使用css_selector和
get_attribute("innerHTML")
:print(driver.find_element(By.CSS_SELECTOR, "table#content td#visitor_id p.otp").get_attribute("innerHTML"))
使用 xpath 和 text 属性:
print(driver.find_element(By.XPATH, "//table[@id='content']//td[@id='visitor_id']//p[@class='otp']").text)
要提取文本 842896 理想情况下,您需要引入 WebDriverWait for the
使用 CSS_SELECTOR 和 text 属性:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "table#content td#visitor_id p.otp"))).text)
使用 XPATH 和
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"))
注意:您必须添加以下导入:
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
参考资料
Link 到有用的文档:
get_attribute()
方法Gets the given attribute or property of the element.
text
属性returnsThe text of the element.
- Difference between text and innerHTML using Selenium