Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 476
IordanouGiannis Avatar asked Oct 23 '25 16:10

IordanouGiannis


1 Answers

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()
like image 76
alecxe Avatar answered Oct 26 '25 06:10

alecxe