Izzat Z.
Izzat Z.

Reputation: 447

Selenium can't locate the element, a search button using xpath

I can't find a solution how this element cannot be found by using a selenium xpath. Other button on other websites always working just fine. Usually what I would normally do is, just open up a page and inspect the element and I would right click it and copy the xpath. Done.

But this website, www.gsc.com.my (a malaysian cinema booking site). Seems not able to find the button. Is it protected by another security layer?

enter image description here

Lets see the code below,

from selenium import webdriver

chromedriver_path = './chromedriver.exe'

driver = webdriver.Chrome(chromedriver_path)  
driver.get('https://www.gsc.com.my')


driver.find_element_by_xpath("""//*[@id="btnNext"]""").click()

The error Message:

no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="btnNext"]"}

Upvotes: 0

Views: 1624

Answers (3)

cruisepandey
cruisepandey

Reputation: 29362

You can try with this css selector :

div.container input#btnNext  

Note that you will need to switch to iframe first , cause the check button is in a iframe.

For switching to iframe :

driver.switch_to.frame(driver.find_element_by_id("getQuickFilter_ctrl_ifrBox"))  

and for clicking on check Button

wait = WebDriverWait(driver, 10)
check_button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.container input#btnNext")))  
check_button.click()

Upvotes: 0

Andersson
Andersson

Reputation: 52665

Button is located inside an iframe, so you need to switch to that frame before clicking the button:

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

driver.switch_to.frame('getQuickFilter_ctrl_ifrBox')

wait(driver, 10).until(EC.element_to_be_clickable((By.ID, "btnNext"))).click()

Upvotes: 1

Bouke
Bouke

Reputation: 1577

Because there are two elements with id btnNext, you'll have to specify which of them using an index, 1 for the first, 2 for the second.

driver.find_element_by_xpath("""//*[@id="btnNext"][1]""").click()

Upvotes: 0

Related Questions