【问题标题】:Cannot navigate to button (web scraping) - selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element无法导航到按钮(网页抓取) - selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素
【发布时间】:2019-09-23 14:31:58
【问题描述】:

我正在使用 selenium 进行一些网页抓取,但我无法让程序点击某个按钮。这是具体的错误信息:

selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素

我不确定出了什么问题(我尝试了以下方法来尝试导航到按钮,包括:

driver.find_element_by_name("HTML-NAME").click()
driver.find_element_by_class("HTML-CLASS").click()
driver.find_element_by_id("HTML-ID").click()
driver.find_element_by_xpath("//a[@id='HTML-ID']").click()
driver.find_element_by_link_text("HTML-LINK-TEXT").click()

这是我尝试单击的按钮的相应 HTML 代码:

< a name="HTML-NAME" id="HTML-ID" ptlinktgt="CODE"tabindex="30" onclick="javascript:cancelBubble(event);" href="javascript:submitAction....." class="HTML-CLASS">BTN-TEXT </a

# My code right now

driver = webdriver.Chrome(executable_path="path/chromedriver")
url = "https://...."

driver.get(url)
time.sleep(5)

input_bar1 = driver.find_element_by_xpath("//input[@id='inp1']")
input1 = input("Username: ")
input_bar1.send_keys(username)

input_bar2 = driver.find_element_by_xpath("//input[@id='inp2']")
input2 = input("Password: ")
input_bar2.send_keys(password)

input_bar2.submit()
time.sleep(5)

elem = driver.find_element_by_link_text("BTN-TEXT")
elem.click()
time.sleep(20)

【问题讨论】:

  • 你好像少了一个尾随空格
  • 对不起,我是这方面的初学者,你指的是什么尾随空格?
  • “BTN-TEXT”(末尾有多余的空格)
  • 您是否检查过链接是否存在于 iframe 中?
  • 有公开网址分享吗?

标签: python selenium selenium-webdriver web-scraping beautifulsoup


【解决方案1】:

我会建议您增加提交后的等待时间。 可能浏览器必须加载一些东西(在提交之后),并且您试图在元素在 DOM 中生成之前单击它。 (提示:wait(number) 中的数字是以毫秒为单位的时间。因此,您在尝试单击 BTN-TEXT 元素之前等待 5 毫秒。尝试增加此数字。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    • 2021-01-25
    相关资源
    最近更新 更多