1

I want to access a button inside a 'div' tag but the problem is, there are two 'div' tags with same class name and one of them has the button. So how to identify which one has the button and access it. So far I tried to solve but I always get 'Unable to locate element' for button.

One is:

div class="weEq5" style="will-change; width;"

Another one is:

div class="weEq5"
    button class="_35EW6"
1
  • 2
    Share your code and HTML (with markup, not as just simplified text) Commented Dec 24, 2018 at 23:02

2 Answers 2

1

To find div contain button, first you need to select the button then go to parent element which is div

driver.find_elements_by_xpath('//button/parent::div[@class="weEq5"]')
# or
driver.find_elements_by_xpath('//button[@class="_35EW6"]/parent::div[@class="weEq5"]')
Sign up to request clarification or add additional context in comments.

2 Comments

You seem to be using BeautifulSoup syntax while OP is looking for Selenium solution
my bad it is because I have multiple tab, @Andersson thanks for telling.
0

To access the button inside the <div> you can use either of the Locator Strategies:

  • Using CSS_SELECTOR:

    myElement = driver.findElement(By.cssSelector("div[class]:not(style)>button"))
    
  • Using XPATH:

    myElement = driver.find_element_by_xpath("//div[@class and not (@style)]/button")
    

Comments

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.