selenium python 点击​​元素没有反应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18421280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:41:40  来源:igfitidea点击:

selenium python click on element nothing happens

pythonselenium

提问by Aaron Lam

I am trying to click on the Gmail link on the Google frontpage in Selenium with the WebDriver on Python. My code basically replicates the one found here: Why Cant I Click an Element in Selenium?

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

My Code:

我的代码:

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()

The webdriver loads the page and then nothing happens. I've tried using the ActionChains and move_to_element(element), click(element), then perform() but nothing happens either.

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

采纳答案by swietyy

Use find_element_by_idmethod:

使用find_element_by_id方法:

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

or correct your xpath to:

或将您的 xpath 更正为:

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

Here you have nice tutorial.

在这里你有很好的教程。

回答by swietyy

try this because i can't see this id in the html:

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

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

回答by A McGuinness

Try

尝试

from selenium.webdriver.common.action_chains import ActionChains

ActionChains(driver).move_to_element(button).click(button_sub).perform()

You can also use ClickElement.

您也可以使用ClickElement.