如何使用 Selenium 和 Python 从父 div 节点中的文本节点中提取文本 5
How to extract the text 5 from the text node within the parent div node using Selenium and Python
我正在尝试从 <div>
中提取文本,这是一个带有 XPath
的内部双引号(请参阅屏幕截图)
<div class="two columns" style="font-size: 18px; padding-top: 5px;">
<img src="images/spins_s.png" border="0" style="margin-bottom: -1px;">
5
</div>
我在这里附上截图,
[ https://i.stack.imgur.com/uOmeS.png][1]
当我在 cole 中尝试这个 xpath 表达式时,记录它的 return correct o/p,
>>$x("//div[@class='two columns']/img[@src='images/spins_s.png']/..")[0].innerText
>>" 5"
但是当我要 运行 这个 code In python
spin_balance = browser.find_element_by_xpath("//div[@class='two columns']/img[@src='images/spins_s.png']/..")[0].innerText
它给出 错误 像这样
spin_balance = browser.find_element_by_xpath("//div[@class='two columns']/img[@src='images/spins_s.png']/..")[0].innerText
TypeError: 'WebElement' object is not subscriptable
HTML 的图像:
文本 5 位于文本节点内。因此,要打印文本,您必须引入 for the visibility_of_element_located()
and you can use either of the following :
使用 CSS_SELECTOR
和 text 属性:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.two.columns"))).text)
使用 XPATH
和 text 属性:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='two columns']"))).text)
使用 XPATH
和 childNodes:
print(driver.execute_script('return arguments[0].lastChild.textContent;', WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='two columns'][./img[@src='images/spins_s.png']]")))).strip())
使用 XPATH
和 splitlines()
:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='two columns'][./img[@src='images/spins_s.png']]"))).get_attribute("innerHTML").splitlines()[2])
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
我正在尝试从 <div>
中提取文本,这是一个带有 XPath
<div class="two columns" style="font-size: 18px; padding-top: 5px;">
<img src="images/spins_s.png" border="0" style="margin-bottom: -1px;">
5
</div>
我在这里附上截图, [ https://i.stack.imgur.com/uOmeS.png][1] 当我在 cole 中尝试这个 xpath 表达式时,记录它的 return correct o/p,
>>$x("//div[@class='two columns']/img[@src='images/spins_s.png']/..")[0].innerText
>>" 5"
但是当我要 运行 这个 code In python
spin_balance = browser.find_element_by_xpath("//div[@class='two columns']/img[@src='images/spins_s.png']/..")[0].innerText
它给出 错误 像这样
spin_balance = browser.find_element_by_xpath("//div[@class='two columns']/img[@src='images/spins_s.png']/..")[0].innerText
TypeError: 'WebElement' object is not subscriptable
HTML 的图像:
文本 5 位于文本节点内。因此,要打印文本,您必须引入 visibility_of_element_located()
and you can use either of the following
使用
CSS_SELECTOR
和 text 属性:print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.two.columns"))).text)
使用
XPATH
和 text 属性:print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='two columns']"))).text)
使用
XPATH
和 childNodes:print(driver.execute_script('return arguments[0].lastChild.textContent;', WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='two columns'][./img[@src='images/spins_s.png']]")))).strip())
使用
XPATH
和splitlines()
:print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='two columns'][./img[@src='images/spins_s.png']]"))).get_attribute("innerHTML").splitlines()[2])
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC