【问题标题】:Unable to locate element with python and selenium无法使用 python 和 selenium 定位元素
【发布时间】:2020-12-07 20:19:05
【问题描述】:

我想点击表示下一步的按钮,它是由'بعدی'写的
在这个页面中
https://www.tgju.org/profile/price_dollar_rl/history
这是我的代码

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.add_argument("--headless")
browser= webdriver.Firefox(options=options, executable_path="geckodriver.exe")
browser.get('https://www.tgju.org/profile/price_dollar_rl/history');
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
wait = WebDriverWait(browser,20)
from selenium.webdriver.common.by import By
browser.find_element_by_xpath('//*[@id="DataTables_Table_0_next"]')

我得到了这个错误
无法定位元素://*[@id="DataTables_Table_0_next"]
但我复制了确切的 ID 表格检查
谢谢

【问题讨论】:

  • 删除options.add_argument("--headless")后会发生什么?
  • @DebanjanB 它不起作用
  • 所以我们需要先解决没有options.add_argument("--headless")的问题,然后再进行headless。

标签: python selenium web-scraping


【解决方案1】:

您需要添加等待元素出现。在此页面上,它看起来像是动态加载的。

element = WebDriverWait(browser, 10).until(
        EC.presence_of_element_located((By.ID, "DataTables_Table_0_next"))
    )

【讨论】:

  • 引发错误是 'TimeoutException(message, screen, stacktrace)' @DMart
  • 我通过本地主机在我的系统中抓取它,它可以工作,谢谢@DMart
  • 我的回答有帮助吗?如果是,则将其标记为正确。有点不清楚您的上一条消息是什么意思。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多