0
votes

I want to Find the XPATH/CSS locator to extract the text from the following structure. Kindly help.

<div class="page-header song-wrap">
<div class="art solo-art">
<div class="meta-info">
<h1 class="page-title">
Zehnaseeb

I want to give the locator/XPATH so that it can return the text "Zehnaseeb" (In this case)

This did not yield any result,

driver.findElement(By.xpath(".//*[@id='main']/div/section/div[1]/div[2]/h1")).getText();

3
is the h1 tag closed after "Zehnaseeb"? - skhro87
also you should specify the code language you are using. c#? java? - skhro87
The h1 tag is not closed after Zehnaseeb, The complete TAG IS HERE,1 class="page-title"> Zehnaseeb <a class="pill star " onclick="Content.toggleFavourites('ZZ4mj40V','hindi', this, $(this).parent().parent().parent().find('.song-json').html() ); this.blur(); void(null);" onmousedown="this.blur();" title="Add to Starred Songs"> </h1> - Shyam

3 Answers

0
votes

If you are using C#, I recommend to use "ScrapySharp", it's very nice for parsing HTTML.

https://bitbucket.org/rflechner/scrapysharp/wiki/Home

Document htmlDoc = new HtmlDocument();
htmlDoc.loadHtml(driver.PageSource);
var zehnaseebstring = doc.DocumentNode.CssSelect("h1.page-title").SingleOrDefault().InnerText;

this should work.

0
votes

I would check all the elements in between to see that the hierarchy is correct, but you could try to simplify by removing some of the elements in between by using descendant //

//*[@id='main']//h1[@class='page-title']
0
votes

have you tried waiting for the element,

String text = new WebDriverWait(driver,30).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.page-header h1.page-title"))).getText();