user3855849 user3855849 - 5 months ago 10
HTML Question

Data not capturing using Selenium web driver

I am trying to capture the product description from a webpage using ID and tagname but when I print it, it is showing blank. However I think I have used correct locators to locate the element.

Page Source

<div id="item-description-block" class="layout-container layout-container-background clearfix">
<h2>About this item</h2>
<div id="social_share" class="details-row" onclick="javascript:clickPDP('Share','123070751499');">
<div class="details-row clear_both">
<div id="inspirational_copy">
<p>The big plus: Our new formula now helps strengthen skin's own moisture barrier. More moisture stays in. Skin feels soft, springy. Has a healthy-looking glow.</p>
</div>


Web Driver Code

driver.get("http://www.debenhams.com/webapp/wcs/stores/servlet/prod_10701_10001_123070751499_-1");
WebElement description =driver.findElement(By.id("inspirational_copy").tagName("p"));
String description1 = description.getText();

Answer

I just tested it in Python and if you replace By.id("inspirational_copy").tagName("p") with a valid css selector you can then use getText() to get the text you're looking for.

driver.get("http://www.debenhams.com/webapp/wcs/stores/servlet/prod_10701_10001_123070751499_-1");
WebElement description =driver.findElement(By.cssSelector("div[id='inspirational_copy']>p"));
String description1 = description.getText();

I did notice when I arrived on the page I got a welcome message. This message prevented me from getting the text. After closing it I could get the element and the text without problems.

WebElement close = driver.findElement(By.cssSelector("button[title='Close']");
close.click()