Home > Enterprise >  Cant click option in options wrapper - python selenium
Cant click option in options wrapper - python selenium

Time:11-06

I am having trouble selecting an option in popup-bubble (sorry I am not quite sure how to determine what type of popup it is) I dont really mind what option I choose, I just need to click one of them. This is how it looks in code:

<div data-v-d4e6e290="" class="f_input-optionsWrapper js_optionsWrapper" style=""><div data-v-d4e6e290="" class="f_input-options"><div data-v-d4e6e290="" class="f_input-header js_header">Oslovení</div> <div data-v-d4e6e290="" class="f_input-options-content"><div class="f_customScroll" data-v-d4e6e290="" style="max-height: 883px;"><div data-v-68c7351e="" class="f_input-option"><div data-v-68c7351e="" data-testid="Pan" class="f_input-option-text xh-highlight">
        Pan
    </div></div><div data-v-68c7351e="" class="f_input-option"><div data-v-68c7351e="" data-testid="Paní" class="f_input-option-text">
        Paní
    </div></div></div> <div data-v-d4e6e290="" class="f_input-footer js_footer"><!----> <span data-v-d4e6e290="" class="f_button f_button--common f_button_set--small">Potvrdit</span></div></div></div></div>

This is the popup box

I tried to locate the elements multiple ways, the first problem is that it always detects two results. The popup is to choose the sex of the traveler and I have two travelers so my guess is it detects the second popup for second traveler as well.

My tries to locate:

volbaOsloveni = driver.find_elements_by_xpath("//*[@class='f_input-optionsWrapper js_optionsWrapper'] //*[@class='f_input-option-text']")
volbaOsloveni2 = driver.find_elements_by_xpath("//*[@data-testid='Pan']")
volbaOsloveni3 = driver.find_element_by_xpath("//*[@class='f_customScroll'] //*[@data-testid='Pan']")

All of these gets me two results which I guess should work. Whenever I try to click the element selenium either says that it didnt locate the element or it just passes it acting as it already clicked the element and the script continues.

Click tries:

volbaOsloveni[0].click()
driver.execute_script("arguments[0].click();", volbaOsloveni3)
volbaOsloveni2[1].click()

I tried the find_elementS method so I am getting more of the results so that is why I tried to click on the first element with [0] and so on. Neither the execute_script or the click() does the job and I am kinda desperate what to do now.

Can anyone help me with that please? Would be greatly appreciated! Thanks in advance

CodePudding user response:

To click on the element with text as Pan you can use either of the following Locator Strategies:

  • Using css_selector:

    driver.find_element(By.CSS_SELECTOR, "div.f_input-options div.f_input-options-content div[data-testid='Pan']").click()
    
  • Using xpath:

    driver.find_element(By.XPATH, "//div[@class='f_input-options']//div[@class='f_input-options-content']//div[@data-testid='Pan']").click()
    

The desired element is a dynamic element, so ideally, to click on the 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, "div.f_input-options div.f_input-options-content div[data-testid='Pan']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='f_input-options']//div[@class='f_input-options-content']//div[@data-testid='Pan']"))).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
    

CodePudding user response:

to click on Pan or Paní

Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.

xpath that you should check :

//div[@data-testid='Paní']

Steps to check:

Press F12 in Chrome -> go to element section -> do a CTRL F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.

If we have 1/1 matching node, Please use any one the mentioned code below to make a click :

Code trial 1 :

time.sleep(5)
driver.find_element_by_xpath("//div[@data-testid='Paní']").click()

Code trial 2 :

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@data-testid='Paní']"))).click()

Code trial 3 :

time.sleep(5)
button = driver.find_element_by_xpath("//div[@data-testid='Paní']")
driver.execute_script("arguments[0].click();", button)

Code trial 4 :

time.sleep(5)
button = driver.find_element_by_xpath("//div[@data-testid='Paní']")
ActionChains(driver).move_to_element(button).click().perform()

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.action_chains import ActionChains

Also, if you still see 2 elements for the XPath

//div[@data-testid='Paní']

please use XPath indexing :

(//div[@data-testid='Paní'])[2]

to filter out the nodes based on index.

  • Related