Unable to find element using Selenium in Python

Viewed 47

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

3 Answers

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
    
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()

Thanks to all. But I figure it out the issue. The button was inside an iFrame. So I hadd to switch to the iFrame first.

Code used:

iframe = driver.find_element_by_id('Access_Performance_Monitor')
driver.switch_to.frame(iframe)
Related