0

enter image description here

I can't get xpath of this html as it gives me error and the test case gives failure

I tried by class name of it's outer div or button

WebDriverWait wait2 = new WebDriverWait (ChromeBroswerObject, 5);
        WebElement element2 = wait2.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"root\"]/div/div/div[3]/div/div[2]/section[2]/div/div[1]/a/p[1]")));
        element2.sendKeys(Keys.ENTER);[enter image description here][1]
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
Mayar
  • 1
  • 3
  • 3
    Link pls if u can, then I can create xpath. – IPolnik Aug 06 '19 at 19:40
  • https://www.bestbuy.ca - Login [With already existing account] - Add any number of items into Shopping Cart - Follow all the needed steps to finish Payment Operation "Check Out" (except last step) [May be you need to view Shopping cart first to do Checkout] - Sign Out . this what should i do and the question is a part of it – Mayar Aug 07 '19 at 12:32

5 Answers5

0

It's a bit hard to tell exactly what you're trying to click, but here is my best guess:

css selector:

p.offerCta_Bis60

Xpath:

//p[@class='offerCta_Bis60']

If that's the correct element, but the class attribute changes on you, you could try this xpath:

//p and ./span[text()= 'Shop Now']

For a specific product, you can add text for that, for example this would turn the second example into this:

//a[contains(p/text(), 'Epic sound for epic study sessions.')]/p and ./span[text()= 'Shop Now']

Explanation:

You should identify some attribute you can rely on that will uniquely identify the element, preferably using an id or class.

In the second example, I'm scanning the entire document(//) looking for a p node that also has a child(./) span with the text Shop Now.

mrfreester
  • 1,876
  • 2
  • 16
  • 33
0

If you like to click on all shop now links, then try this,

List<WebElement> links = driver.findElements(By.xpath("//p/span[contains(.,'Shop now')]"));
for(WebElement link : links)
        {
            link.click();
        }

Or if you like just the first shop now link,

WebElement element2 = wait2.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[contains(.,'Epic sound for epic study sessions.')]/p/span[contains(.,'Shop now')]")));
        element2.click();
Sureshmani
  • 1,849
  • 2
  • 6
  • 17
0

You can use this below xpath.

Make sure to replace x with the desired item number.

(//section[@data-automation='dynamic-content-offer-list']//div[contains(@class,'ItemsPerRow')])[x]

Screenshot: enter image description here

If you want to get all the items in the list and then iterate through them then you can use below xpath with findElements.

//section[@data-automation='dynamic-content-offer-list']//div[contains(@class,'ItemsPerRow')]

If you want to get the paragraph under link, then simply extend the xpath as below.

(//section[@data-automation='dynamic-content-offer-list']//div[contains(@class,'ItemsPerRow')]/a/p)[1]
supputuri
  • 12,238
  • 2
  • 14
  • 34
0

To click() on the element with text as Shop now as the element is a dynamic element you have to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("ta[href*='epic-sound-for-epic-study-sessions'] p[class^='offerCta_']>span"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[contains(@href, 'epic-sound-for-epic-study-sessions')]//p[starts-with(@class, 'offer')]/span[text()='Shop now']"))).click();
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
0
  1. First locate the parent <p> element preferably by partial text using XPath contains() function
  2. Then locate following-sibling (next node having the same parent)
  3. And finally filter the resulting <span> by its text

    //p[contains(text(), 'Save up to')]/following-sibling::p/span[text()='Shop now']
    

References:

Dmitri T
  • 119,313
  • 3
  • 56
  • 104