【问题标题】:Can't select element from page [Selenium[无法从页面 [Selenium [
【发布时间】:2022-01-16 00:48:13
【问题描述】:

我已尝试从该站点中删除信息 - 特别是从表格中。每次我出现,元素不存在的信息。

https://polygonscan.com/token/0x64a795562b02830ea4e43992e761c96d208fc58d

我尝试将time.slep(5) 添加到我的代码或向下滚动功能以加载所有元素 - 无效。

你对我有什么建议吗?

编辑

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException


# Options
chrome_options = Options()
chrome_options.add_argument("--headless")

# Set drive
chrome_driver_path = r"C:\Users\kacpe\OneDrive\Pulpit\Python\Projekty\chromedriver.exe"
driver = webdriver.Chrome(chrome_driver_path, options=chrome_options)
driver.get("https://polygonscan.com/token/0x64a795562b02830ea4e43992e761c96d208fc58d")


try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, "//table/tbody/tr[0]")))
    print(element)
except TimeoutException as e:
    print(e)

我根据您的请求添加了代码。所以我的主要目标是从这个站点的表格中删除内容。我将显式等待添加到我的代码中,但我仍然无法从该表中选择任何内容 - 看起来脚本没有看到该区域的任何内容。

【问题讨论】:

    标签: python selenium web-scraping


    【解决方案1】:

    尝试解决它的一种方法,它使用元素的 Xpath 或相同元素的相对位置,使 Selenium 始终获得相同的位置“线”以返回您所在信息的“值”搜索。

    Ex1:find_element(:xpath,"//*[@id="wmd-input"]")#这样的话就是这个复选框的输入。

    如果它不起作用,试试这个。

    Ex2: browser.implicitly_wait(30) #制作一个计时器,将所有信息从网络加载到您的机器上。

    【讨论】:

    • 第一:请发Minimum Reproducible Code 以有效地调查问题。第二:可以通过两种方式进行抓取,一种使用 bs4,另一种使用 Selenium(或两者都使用),但您没有说明您使用的是什么。按照标签 selenium,我会说你可以使用 WebdriverWaitvisibility_of_element_located。这将帮助您等待设置的超时,同时检查目标元素的可见性
    • 虽然implicitly_wait 有效,但在扩展代码时不建议/不推荐。 Implicitly_wait 一旦设置必须关闭,否则它将应用于整个代码,这会影响整体性能。因此,应该首选诸如WebdriverWait 之类的显式等待,其功能将在其工作完成时终止。
    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    • 我已经编辑了我的原始帖子。你的建议不起作用
    猜你喜欢
    • 1970-01-01
    • 2018-08-13
    • 2021-10-08
    • 1970-01-01
    • 2016-03-19
    • 2012-08-15
    • 2016-09-19
    • 2016-03-19
    • 1970-01-01
    相关资源
    最近更新 更多