Home > Back-end >  Xpath locator unable to detect the element
Xpath locator unable to detect the element

Time:11-05

I am trying to select a button using selenium however i believe i am doing something wrong while writing my xpath can anyone please help me on this i need to select the currency Euro. link :- enter image description here

Locator which i have written

enter image description here

USD = self.find_element_by_xpath(f"//a[contains(text(),'selected_currency='Euro']")
USD.click()

CodePudding user response:

As already explained, selected_currency=EUR is in the attribute - data-modal-header-async-url-param.

However you can select the required option, with below code.

The xpath for the EUR option can be - //div[contains(text(),'EUR')]. Since it highlights 2 elements in the DOM using this xpath- (//div[contains(text(),'EUR')])[1]. Its important to find unique locators. Link to refer

# Imports required for Explicit wait
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver.get("https://www.booking.com/")

wait = WebDriverWait(driver,30)

# Click on Choose currency option
wait.until(EC.element_to_be_clickable((By.XPATH,"//span[@class='bui-button__text']/span[contains(text(),'INR')]"))).click()

# Click on the EUR option.
euro_option = wait.until(EC.element_to_be_clickable((By.XPATH,"(//div[contains(text(),'EUR')])[1]")))
euro_option.click()

CodePudding user response:

The text you are looking for: selected_currency='EUR' is in the data-modal-header-async-url-param parameter of the a tag. You should run the contains on that.

Edit: Locator: //a[contains(@data-modal-header-async-url-param, 'selected_currency=EUR')]

  • Related