jitlp
jitlp

Reputation: 47

Cannot find element using xpath (Python and selenium)

I try to make some automations using Python+selenium (new to this). Unfortunatelly, inspecting elements of a specific webpage is more than hard. There is no id to use and I try xpath. I want to select a drop down list , i inspect this element and I copy the xpath which is //*[@id="frmMain:criteria:purchase_criteria_tab"]/div[13]/div[1]/div/button

my code is:

    NEXT_BUTTON_XPATH = "//*[@id='frmMain:criteria:purchase_criteria_tab']/div[13]/div[1]/div/button"
wait = WebDriverWait(driver, 10)
condition = expected_conditions.presence_of_element_located(
    (By.XPATH, NEXT_BUTTON_XPATH))
button = wait.until(condition)
button.click()

and I cannot get the element Any ideas? Thank you in advance.

Upvotes: 0

Views: 554

Answers (2)

Manali Kagathara
Manali Kagathara

Reputation: 761

try this

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait 
try:
    # for click element element_to_be_clickable condition used
    button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable(By.XPATH, "elemnt_xpath"))) 
    button.click()

except Exception as e:
    print(e)

Upvotes: 1

Sameer Arora
Sameer Arora

Reputation: 4507

You can click on the element using the syntax:

WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//*[@id='frmMain:criteria:purchase_criteria_tab']/div[13]/div[1]/div/button"))).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

Upvotes: 0

Related Questions