Home > Back-end >  I can't clicking on the label button with javascripts
I can't clicking on the label button with javascripts

Time:11-05

I have 1 question that I can't clicking on the button with javascript. I use

driver.find_element(By.XPATH, "//li[@class='login']/a")

but it doesn't work at all. it seems like javascripts.

enter image description here

Plz help me to clicking on the login button.

enter image description here

CodePudding user response:

This might be more helpful if you can provide more of the source code or maybe a link to the site?

But from what I see. Did you actually click?

driver.find_element(By.XPATH, "//li[@class='login']/a").click()

CodePudding user response:

You need to wait for the element to be clickable (remember to add proper imports), and here is a working code:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://www.oliveyoung.co.kr")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[@class='login']/a"))).click()
input()
driver.close()

CodePudding user response:

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

xpath that you should check :

//li[@class='login']//a

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.

If we have 1/1 matching node, Please make sure that :

  1. This div is not under an iframe.
  2. This div is not under a shadow-root.
  3. You should not be on new tab/windows launched by selenium.

if it matches the desired element, then in Selenium there are 4 ways to click on it.

Code trial 1 :

time.sleep(5)
driver.find_element_by_xpath("//li[@class='login']//a").click()

Code trial 2 :

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[@class='login']//a"))).click()

Code trial 3 :

time.sleep(5)
button = driver.find_element_by_xpath("//li[@class='login']//a")
driver.execute_script("arguments[0].click();", button)

Code trial 4 :

time.sleep(5)
button = driver.find_element_by_xpath("//li[@class='login']//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
  • Related