我编写了一个脚本,它使用selenium和pyautogui模块登录并从元素中抓取一个值并打印出来,但它正在打印两个破折号--。
下面是包含我要检索的值417的HTML:
<p id="totReqCountVal" class="trailer-0 avenir-regular font-size-4 text-green js-total-requests">417</p>这是我尝试过的相关代码:
from selenium import webdriver
from selenium.webdriver.common.by import By
browser.get('website_to_be_scraped')
browser.find_element(By.ID, 'totReqCountVal')然后我试着:
views = browser.find_element(By.ID, 'totReqCountVal')
print(views)返回:
(session="12e48df447f7df855a1ee596ba609a30", element="1027ec31-8cb8-4758-b4b0-82b85628ed6c")在一些帮助下,我还尝试了以下几点:
使用CSS_SELECTOR和text属性:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "p#totReqCountVal[class$='js-total-requests']"))).text)
Using XPATH and get_attribute("innerHTML"):
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//p[@id='totReqCountVal' and contains(@class, 'js-total-requests')]"))).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我已经检查了devtools,如果定位器策略唯一地标识元素,检查iframes和阴影根。
如何检索417值?
发布于 2022-02-10 19:23:20
views是在打印时正确打印的WebElement:
(session="12e48df447f7df855a1ee596ba609a30", element="1027ec31-8cb8-4758-b4b0-82b85628ed6c")解决方案
要打印文本417,您需要为导出WebDriverWait,您可以使用以下任何一个Locator Strategies
打印( 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR,"p#totReqCountValclass$='js-total-requests'"))).text),驱动程序,WebDriverWait)
使用XPATH和get_attribute("innerHTML")的
打印( 20).until(EC.visibility_of_element_located((By.XPATH,(驱动程序,WebDriverWait "//p@id='totReqCountVal‘)并包含(@WebDriverWait,'js-total-requests')"))).get_attribute("innerHTML")) )
从selenium.webdriver.support.ui导入WebDriverWait从selenium.webdriver.common.by导入从selenium.webdriver.support导入expected_conditions作为EC
您可以在How to retrieve the text of a WebElement using Selenium - Python中找到相关的讨论
参考文献
链接到有用的文档:
get_attribute()方法Gets the given attribute or property of the element.text属性返回The text of the element.https://stackoverflow.com/questions/71070759
复制相似问题