1

I'm trying to click the one-way button on this site. I'm waiting for the button to be clickable, but the script times out. Here's the code:

with webdriver.Chrome() as driver:
    driver.get("https://www.united.com/en/us")
    WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#oneway'))).click()

Thanks!

I made the script work by using the label in-place of the input itself:

    WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '[for="oneway"]'))).click()

But I'm not able to understand why the first approach doesn't work.

2 Answers2

0

The desired element is a dynamic element, so 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:

    driver.get("https://www.united.com/en/us")
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label[for='oneway']"))).click()
    
  • Using XPATH:

    driver.get("https://www.united.com/en/us")
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//label[@for='oneway']"))).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
    
  • Browser Snapshot:

One-way

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thanks for answering. But my question is why does waiting for #oneway not work, but waiting for the label (in your answer too) work? I’m able to select and click #oneway from JS, but doing so from Python times out (element_to_be_clickable) –  Jun 19 '23 at 02:39
0

If you inspect each element i.e. <label> and <input> in dev-tools and notice, you will see that there is a difference in the co-ordinates where they are located. This could be the reason why clicking label works whilst clicking input doesn't. Refer below images, you will get an idea.

  • Notice the position of <label> where it locates:

enter image description here

  • Notice the position of <input> where it locates:

enter image description here

Shawn
  • 4,064
  • 2
  • 11
  • 23
  • From what I'm aware of, Selenium simulates a click action by interacting with the DOM itself, so screen coordinates should not be an issue. I'll be more than happy to be corrected. Thanks! –  Jun 19 '23 at 13:03
  • Yes your statement about the function of `click()` method sounds rational and correct. For some reason selenium is not able to locate the `` element. Am not too sure about the root cause of this. Let's wait and see if anyone else comes up with any better reason for selenium not able to find the `input` node. Am upvoting your question. – Shawn Jun 19 '23 at 13:42