简体   繁体   中英

click() works in Chrome but not Firefox?

I want open a site and click on a button, so i wrote the following code:

from selenium import webdriver

driver = webdriver.Firefox()
driver.get('http://python.org')
elem = driver.find_element_by_id("downloads")
elem.click()

The code opens Firefox with the given url. It finds the button by it's id name and then clicks on it.

Now the problem is, it doesn't work properly in Firefox. When i click on the downloads button from the above website, a new link will be opened in my Firefox. But when the code gets executed, it clicks on the button not in the way i do, more like as if it's "selecting" the button and not clicking on it.

The odd thing is that the above code works in a flawless manner with Chrome:

from selenium import webdriver

driver = webdriver.Chrome()
driver.get('http://python.org')
elem = driver.find_element_by_id("downloads")
elem.click()

It clicks on the downloads button then a new link is opened in the Chrome.

I wanted to know, what is the problem? Why does the code work with Chrome but not with Firefox?

By the way, I am running Windows 7 64-bit along with Python 2.7 32-bit. My browsers version are 36 for Firefox and 41 for Chrome.

I see there is a child node, anchor tag for an element you are trying to click. You need to click on anchor tag to navigate to the link. Below code worked for me

driver.find_element_by_css_selector("#downloads a").click()

This is what seems to work for me. Please try:

from selenium import webdriver

driver = webdriver.Firefox()
driver.get('http://python.org')
elem = driver.find_element_by_link_text("Downloads")
elem.click()

And for chrome:

from selenium import webdriver

driver = webdriver.Chrome("res/chromedriver.exe")
driver.get('http://python.org')
elem = driver.find_element_by_link_text("Downloads")
elem.click()

Wait for the Downloads link to become clickable , move to the element and click:

wait = WebDriverWait(driver, 10)
downloads = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Downloads")))

ActionChains(driver).move_to_element(downloads).click().perform()

Works for me in both Chrome and Firefox.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM