Home > Back-end >  Unable to find element using Selenium in Python
Unable to find element using Selenium in Python

Time:07-24

I'm trying to click on a button, but selenium can't find the element.

enter image description here

I'm using the xpath:

//button[@title="Monitor Chart(Ctrl Alt G)"]

but selenium can't locate. I can find the xpath from Chrome manualy using the find(Ctrl F) tool, as we can see in the image.

Here is my code and error. The xpath does't have an ID.

Code Snapshot:

enter image description here

CodePudding user response:

To click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.eui-btn.eui-btn-default.eui-btn-normal[title^='Monitor Chart']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='eui-btn eui-btn-default eui-btn-normal' and starts-with(@title, 'Monitor Chart')]"))).click()
    
  • Note: You have to add the following imports :

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

CodePudding user response:

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

hiding_button = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[title ='Monitor Chart(Ctrl Alt G)']")))
hiding_button.click()
  • Related