简体   繁体   中英

How do I properly click an element in Selenium with Python?

I am trying to click an element, for example "AH", in this page . I use the following code.

from selenium import webdriver

url = "http://www.oddsportal.com/soccer/brazil/serie-a/internacional-santos-vcGTTAKH/"
driver = webdriver.Firefox()
driver.get(url)
element_to_click = driver.find_element_by_link_text("AH")
element_to_click.click()

The problem is that after the element is clicked and the new page is loaded, it goes back to the first page.

Focus the element and call click_and_hold action (worked for me):

from selenium.webdriver import ActionChains

actions = ActionChains(driver)
actions.move_to_element(element_to_click).click_and_hold(element_to_click).perform()

alecxe , that works.

Just to add to the discussion here

在此输入图像描述

So on mouse down it is invoking onClick for the uid(4), when we do a normal click on the element we do not realize that it worked on mouse down not on mouse click .

Thats why when we are using webdriver to do element.click() on it, this does not work and when we use Actions class to simulate mouse down using click_and_hold , It works !!

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