【问题标题】:selenium python click on element nothing happens硒python点击元素没有任何反应
【发布时间】:2013-08-24 17:33:31
【问题描述】:

我正在尝试使用 Python 上的 WebDriver 在 Selenium 中单击 Google 首页上的 Gmail 链接。我的代码基本上复制了这里的代码:Why Cant I Click an Element in Selenium?

我的代码:

import selenium.webdriver as webdriver
firefox = webdriver.Firefox()
firefox.get("http://www.google.ca")
element = firefox.find_element_by_xpath(".//a[@id='gb_23']")
element.click()

webdriver 加载页面,然后什么也没有发生。我尝试使用 ActionChains 和 move_to_element(element)、click(element),然后 perform(),但也没有任何反应。

【问题讨论】:

  • xpath 与 selenium 一起使用从来都不是一件好事。尝试严格按 id 搜索,因为你有它
  • 该代码适用于我..你能测试相同的代码,但使用 chrome 而不是 firefox?
  • 您可以尝试降级 Firefox。我读过新版本的 Firefox 经常会中断 Selenium 交互。
  • 问题是我的工作使用了 firefox,我正在尝试学习 selenium。我正在尝试使用 xpath,因为我将要测试的 Web 应用程序没有我需要单击的元素的 ID...

标签: python selenium


【解决方案1】:

使用find_element_by_id方法:

element = firefox.find_element_by_id("gb_23")
element.click()

或将您的 xpath 更正为:

"//a[@id='gb_23']"

Here you have nice tutorial.

【讨论】:

  • 嗯,奇怪...试试看:firefox.click("xpath=//a[@id='gb_23']")
【解决方案2】:

试试这个,因为我在 html 中看不到这个 id:

driver = webdriver.Firefox()
driver.get("http://www.google.ca")
element = driver.find_element_by_link_text("Gmail")
element.click()

【讨论】:

    【解决方案3】:

    试试

    from selenium.webdriver.common.action_chains import ActionChains
    
    ActionChains(driver).move_to_element(button).click(button_sub).perform()
    

    您也可以使用ClickElement

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-26
      • 2023-03-03
      相关资源
      最近更新 更多