Home > Software engineering >  How to click the below element in selenium webdriver python
How to click the below element in selenium webdriver python

Time:04-16

I would like to click on the below "sign in" option using selenium webdriver in python. Can someone help me on this?

<div  data-name="header-user-menu-sign-in"><div ><svg height="28" viewbox="0 0 28 28" width="28" xmlns="http://www.w3.org/2000/svg"><path d="M17.5 9c0 1.14-.3 1.99-.79 2.54-.46.52-1.27.96-2.71.96s-2.25-.44-2.71-.96A3.74 3.74 0 0 1 10.5 9c0-1.14.3-1.99.79-2.54.46-.52 1.27-.96 2.71-.96s2.25.44 2.71.96c.5.55.79 1.4.79 2.54zM19 9c0 2.76-1.45 5-5 5s-5-2.24-5-5 1.45-5 5-5 5 2.24 5 5zm-8 8.5h6c2.04 0 3.1.5 3.76 1.1.69.63 1.11 1.55 1.5 2.8.13.42.04.95-.29 1.4-.33.46-.8.7-1.22.7H7.25c-.43 0-.89-.24-1.22-.7a1.61 1.61 0 0 1-.3-1.4 6.08 6.08 0 0 1 1.51-2.8c.65-.6 1.72-1.1 3.76-1.1zm6-1.5h-6c-4.6 0-5.88 2.33-6.7 4.96-.58 1.89.97 4.04 2.95 4.04h13.5c1.98 0 3.53-2.15 2.95-4.04C22.88 18.33 21.6 16 17 16z" fill="currentColor" fill-rule="evenodd"></path></svg></div><div ><div >Sign in</div></div></div>

EDIT: I usually find the XPATH and driver.find_element(By.XPATH, button).click() to click on buttons/hyperlinks. In this page, after loading it, I was able to click on the "user" icon using driver.find_element(By.XPATH, "/html/body/div[2]/div[3]/div[2]/div[3]/button[1]").click() after which it shows menu with "sign in" option. I couldnt find the XPATH for that in firefox using "Inspect" option. Then I saved the html page and checked the definition of "sign in" option and didnt find any button/hyperlink definition in it. So, I would like some help on this to proceed further. Thanks

CodePudding user response:

You can click on sing in button using the below CSS_SELECTOR with explicit waits:

Code:

driver.maximize_window()
wait = WebDriverWait(driver, 30)


driver.get("https://in.tradingview.com/")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[aria-label='Open user menu']"))).click()

wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@data-name='header-user-menu-sign-in' and contains(.,'Sign in')]"))).click()

Imports:

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

Screenshot:

enter image description here

  • Related