'Page elements seen in the browser dev tools are not retrieved by ChromeHeadless

In this page :

https://www.bedbathandbeyond.com/store/product/o-o-by-olivia-oliver-turkish-modal-bath-towel-collection/5469128?categoryId=13434

I can see a button with "Add to Cart" text , I can also see it in dev tools.

But when the same page source is retrieved by ChromeHeadless using selenium, and my script searches for it, this text is not present.

I tried with selecting show page source in the browser, the source too did not have the "Add To Cart text"

Further I used a curl to GET page, "Add To Cart" wasn't in the returned page source either.

What am I doing wrong?

is the page hiding the button?

How can I check for its presence, for product availability check?



Solution 1:[1]

The elements you are looking for are inside the shadow DOM. You need to access the shadow root first. Hard to see exactly what is going on in the DOM without some trial and error, but something like this:

WebElement shadowHost = driver.findElement(By.cssSelector("#wmHostPdp"));
SearchContext shadowRoot = shadowHost.getShadowRoot();
WebElement addToCart = shadowRoot.findElement(By.cssSelector(".shipItBtnCont button"));

More info on Shadow DOM & Selenium — https://titusfortner.com/2021/11/22/shadow-dom-selenium.html

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 titusfortner