0

This is the button with classname action. How can I point to only this button inside this snackbar class. I need the xpath.

<div class="snackbar-container  snackbar-pos bottom-center" style="width: 475px; background: rgb(50, 50, 50); opacity: 1;" xpath="1">
   <p style="margin: 0px; padding: 0px; color: rgb(255, 255, 255); font-size: 14px; font-weight: 300; line-height: 1em;">Show similar patients for  Jerry Rocker</p>
   <button class="action" style="color: rgb(76, 175, 80);">SHOW</button>
</div>

Tried this. WebElement

snackbarButton=driver.findElement(By.xpath("//button[contains(@class,'action') and contains(@class,'snackbar-pos']"));

    <div class="snackbar-container  snackbar-pos bottom-center" style="width: 475px; background: rgb(50, 50, 50); opacity: 1;" xpath="1">
       <p style="margin: 0px; padding: 0px; color: rgb(255, 255, 255); font-size: 14px; font-weight: 300; line-height: 1em;">Show similar patients for  Jerry Rocker</p>
       <button class="action" style="color: rgb(76, 175, 80);">SHOW</button>
    </div>
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    Hello and welcome to stackoverflow. This question is really need an example of html that you want to find. Please read [the article](https://stackoverflow.com/help/minimal-reproducible-example) about a minimal, reproducible example – marv255 Aug 23 '19 at 11:28

3 Answers3

0

Presumably you intent to click on the button with text as SHOW associated with the element with text as Show similar patients for Jerry Rocker and to achieve that you to induce WebDriverWait for the element to be clickable and you can use either of the following Locator Strategies:

  • xpath1:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//p[text()='Show similar patients for  Jerry Rocker']//following::button[1]"))).click();
    
  • xpath2:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//p[text()='Show similar patients for  Jerry Rocker']//following::button[@class='action' and text()='SHOW']"))).click();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

If you want to get the button which is a child of <div> tag which class attribute contains snackbar-pos you could go for the following expression:

//div[contains(@class,'snackbar-pos')]/button

Demo:

enter image description here

However it might make more sense to stick to this xpath attribute:

//div[@xpath='1']/button

Or even this Jerry Rocker text:

//p[contains(text(),'Jerry Rocker')]/following-sibling::button

References:

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
0

You can use below given xpath.

//div[@class='snackbar-container  snackbar-pos bottom-center']//child::p//following-sibling::button[@class='action']

Hope this will help.

Zohair
  • 268
  • 2
  • 7