selenium python单击元素什么都不发生

2024-06-17 15:15:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图用Python上的WebDriver点击Selenium中Google frontpage上的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并将_移动到_element(element),单击(element),然后执行(),但也没有发生任何事情。


Tags: 代码an链接seleniumgoogleelementfirefoxgmail
3条回答

使用find_element_by_id方法:

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

或者将xpath更正为:

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

Here you have nice tutorial.

试试看

from selenium.webdriver.common.action_chains import ActionChains

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

您也可以使用ClickElement

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

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

相关问题 更多 >