Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

348
Visualizações
How to click an element with Javascript in Selenium Python

Using selenium, I would like to have a result of an click action on an element in html (HTML4).

The dom in the html is like this

<A HREF="javascript:open_login_page();">
<IMG NAME="login_button" SRC="hoge.jpg" alt="login"></A>

It seems my Python script successfully obtains the element.

Using the obtained element a_element,

a_element.get_attribute('href')

returns

javascript:open_login_page();

However, a_element.click() raises an error.

How can I have the result?

The expected result is to have another window popup.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There are basically 4 ways to click in Selenium.

I will use this xpath

//IMG[@NAME='login_button']//parent::A

Code trial 1 :

time.sleep(5)
driver.find_element_by_xpath("//IMG[@NAME='login_button']//parent::A").click()

Code trial 2 :

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//IMG[@NAME='login_button']//parent::A"))).click()

Code trial 3 :

time.sleep(5)
button = driver.find_element_by_xpath("//IMG[@NAME='login_button']//parent::A")
driver.execute_script("arguments[0].click();", button)

Code trial 4 :

time.sleep(5)
button = driver.find_element_by_xpath("//IMG[@NAME='login_button']//parent::A")
ActionChains(driver).move_to_element(button).click().perform()

Imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

PS : Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.

Steps to check:

Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda