0

Lets say you have an element on a webpage found successfully

parent_element = driver.find_element_by_css_selector(some_css_selector)

I want to enact a WebDriverWait which waits until one of its child elements is clickable. Usually you would do that with

WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.CSS_SELECTOR,some_css_selector)))

however in my case I want some_css_selector to be the child element of parent_element. How can I do this?

Edit: For instance - I have learned that I can do this:

wait = WebDriverWait(driver, 30)
child_element = wait.until(lambda d:parent_element.find_element_by_css_selector('child_selector'))

which seems to work. However this is just waiting until that child element appears. I want to use the expected conditions (such as element_to_be_clickable).

4
  • would be usseful if you provide your url Commented Apr 21, 2020 at 20:30
  • I can't provide a url as its a password protected site Commented Apr 21, 2020 at 20:42
  • 1
    from your question its unclear which element you are trying to access with parent child relationship Commented Apr 21, 2020 at 20:43
  • that's true, maybe I can edit my question to make it non-specific to my use case Commented Apr 21, 2020 at 20:46

2 Answers 2

2

Here you are trying to pass by locator CSS_SELECTOR, so you have to locate using parent-child relationship:

  WebDriverWait(driver,30).until(EC.element_to_be_clickable(By.CSS_SELECTOR,//Parent-selector > child-Selector))

I am able to pass the webelement in java with element_to_be_clickable, i am not sure whether it will work in python or not. You can give it a try.

child_element= parent_element.find_element_by_css_selector('child_selector')
WebDriverWait(driver,30).until(EC.element_to_be_clickable(child-element))
Sign up to request clarification or add additional context in comments.

Comments

0

If you want to handle child element based on presence of parent then you can use sibling function to fetch sibling to the parent web element, in simple words. How to deal with sibling using selenium

Also there another way you can use if loop and check for child based on presence of your parent element.

if parent:
    if child:
        print "do stuff"
    else:
        print "failed to locate child"
else:
     print "failed to locate parent"

2 Comments

I don't quite understand... I know that the parent exists. And I know that the child exists. My question is specifically about using the Expected Conditions for a child selector in a wait. I don't see how that would integrate here.
ok, if you go to google.com and check this //div[@class='szppmdbYutt__middle-slot-promo']//child::a xpath you will get to know is your parent element and in the parent your are trying to locate anchor tag .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.