Reputation: 655
I am trying to get last span's style value from first and second li, using this code
String lastdeal = driver.findElement(By.xpath(".//*[contains(@class,'container')]//ul[1]/div/li/div/span[7]")).getAttribute("style");
below is my html code and there are many div tag before class container.
it is showing unable to locate error.
Please let me know what i am missing
<div class="container">
<ul class="index__game-stats__list">
...
</ul>
<ul class="index__game-stats__table" style="height: 637px;">
<li style="cursor: auto;">...</li>
<div style="display: flex; width: 100%; flex-direction: column;">
<li style="margin-bottom: 0px; opacity: 1;">
<div style="width: 100%; display: flex;"><span class="player-stats__show" style="text-decoration: underline; cursor: pointer;">20,633,915,829</span><span class="player-stats__show" style="text-decoration: underline; cursor: pointer;">rikhisahu</span><span class="player-stats__hide">21:38</span><span class="player-stats__hide">0.00000025</span><span class="player-stats__hide"><!-- react-text: 35897 -->1.48<!-- /react-text --><!-- react-text: 35898 -->x<!-- /react-text --></span><span class="player-stats__hide"><!-- react-text: 35900 --><<!-- /react-text --><!-- react-text: 35901 --> <!-- /react-text --><!-- react-text: 35902 -->67.00<!-- /react-text --></span><span class="player-stats__hide">31.80</span><span class="player-stats__show" style="color: rgb(147, 204, 88);">0.00000011</span></div>
</li>
<li style="margin-bottom: 0px; opacity: 1;">
<div style="width: 100%; display: flex;"><span class="player-stats__show" style="text-decoration: underline; cursor: pointer;">20,633,913,519</span><span class="player-stats__show" style="text-decoration: underline; cursor: pointer;">rikhisahu</span><span class="player-stats__hide">21:37</span><span class="player-stats__hide">0.00000025</span><span class="player-stats__hide"><!-- react-text: 35881 -->1.48<!-- /react-text --><!-- react-text: 35882 -->x<!-- /react-text --></span><span class="player-stats__hide"><!-- react-text: 35884 --><<!-- /react-text --><!-- react-text: 35885 --> <!-- /react-text --><!-- react-text: 35886 -->67.00<!-- /react-text --></span><span class="player-stats__hide">7.56</span><span class="player-stats__show" style="color: rgb(147, 204, 88);">0.00000011</span></div>
</li>
<li style="margin-bottom: 0px; opacity: 1;">
<div style="width: 100%; display: flex;"><span class="player-stats__show" style="text-decoration: underline; cursor: pointer;">20,633,911,707</span><span class="player-stats__show" style="text-decoration: underline; cursor: pointer;">rikhisahu</span><span class="player-stats__hide">21:37</span><span class="player-stats__hide">0.00000050</span><span class="player-stats__hide"><!-- react-text: 35865 -->1.48<!-- /react-text --><!-- react-text: 35866 -->x<!-- /react-text --></span><span class="player-stats__hide"><!-- react-text: 35868 --><<!-- /react-text --><!-- react-text: 35869 --> <!-- /react-text --><!-- react-text: 35870 -->67.00<!-- /react-text --></span><span class="player-stats__hide">6.51</span><span class="player-stats__show" style="color: rgb(147, 204, 88);">0.00000023</span></div>
</li>
<li style="margin-bottom: 0px; opacity: 1;">
<div style="width: 100%; display: flex;"><span class="player-stats__show" style="text-decoration: underline; cursor: pointer;">20,633,909,553</span><span class="player-stats__show" style="text-decoration: underline; cursor: pointer;">rikhisahu</span><span class="player-stats__hide">21:37</span><span class="player-stats__hide">0.00000025</span><span class="player-stats__hide"><!-- react-text: 35849 -->1.48<!-- /react-text --><!-- react-text: 35850 -->x<!-- /react-text --></span><span class="player-stats__hide"><!-- react-text: 35852 --><<!-- /react-text --><!-- react-text: 35853 --> <!-- /react-text --><!-- react-text: 35854 -->67.00<!-- /react-text --></span><span class="player-stats__hide">81.56</span><span class="player-stats__show" style="color: rgb(255, 117, 145);">-0.00000025</span></div>
</li>
<li style="margin-bottom: 0px; opacity: 1;">
<div style="width: 100%; display: flex;"><span class="player-stats__show" style="text-decoration: underline; cursor: pointer;">20,633,906,424</span><span class="player-stats__show" style="text-decoration: underline; cursor: pointer;">rikhisahu</span><span class="player-stats__hide">21:37</span><span class="player-stats__hide">0.00000100</span><span class="player-stats__hide"><!-- react-text: 35833 -->1.48<!-- /react-text --><!-- react-text: 35834 -->x<!-- /react-text --></span><span class="player-stats__hide"><!-- react-text: 35836 --><<!-- /react-text --><!-- react-text: 35837 --> <!-- /react-text --><!-- react-text: 35838 -->67.00<!-- /react-text --></span><span class="player-stats__hide">20.96</span><span class="player-stats__show" style="color: rgb(147, 204, 88);">0.00000047</span></div>
</li>
<li style="margin-bottom: 0px; opacity: 1;">
<div style="width: 100%; display: flex;"><span class="player-stats__show" style="text-decoration: underline; cursor: pointer;">20,633,904,236</span><span class="player-stats__show" style="text-decoration: underline; cursor: pointer;">rikhisahu</span><span class="player-stats__hide">21:37</span><span class="player-stats__hide">0.00000050</span><span class="player-stats__hide"><!-- react-text: 35817 -->1.48<!-- /react-text --><!-- react-text: 35818 -->x<!-- /react-text --></span><span class="player-stats__hide"><!-- react-text: 35820 --><<!-- /react-text --><!-- react-text: 35821 --> <!-- /react-text --><!-- react-text: 35822 -->67.00<!-- /react-text --></span><span class="player-stats__hide">74.09</span><span class="player-stats__show" style="color: rgb(255, 117, 145);">-0.00000050</span></div>
</li>
<li style="margin-bottom: 0px; opacity: 1;">
<div style="width: 100%; display: flex;"><span class="player-stats__show" style="text-decoration: underline; cursor: pointer;">20,633,902,510</span><span class="player-stats__show" style="text-decoration: underline; cursor: pointer;">rikhisahu</span><span class="player-stats__hide">21:37</span><span class="player-stats__hide">0.00000025</span><span class="player-stats__hide"><!-- react-text: 35801 -->1.48<!-- /react-text --><!-- react-text: 35802 -->x<!-- /react-text --></span><span class="player-stats__hide"><!-- react-text: 35804 --><<!-- /react-text --><!-- react-text: 35805 --> <!-- /react-text --><!-- react-text: 35806 -->67.00<!-- /react-text --></span><span class="player-stats__hide">68.09</span><span class="player-stats__show" style="color: rgb(255, 117, 145);">-0.00000025</span></div>
</li>
<li style="margin-bottom: 0px; opacity: 1;">
<div style="width: 100%; display: flex;"><span class="player-stats__show" style="text-decoration: underline; cursor: pointer;">20,633,900,413</span><span class="player-stats__show" style="text-decoration: underline; cursor: pointer;">rikhisahu</span><span class="player-stats__hide">21:37</span><span class="player-stats__hide">0.00000050</span><span class="player-stats__hide"><!-- react-text: 35785 -->1.48<!-- /react-text --><!-- react-text: 35786 -->x<!-- /react-text --></span><span class="player-stats__hide"><!-- react-text: 35788 --><<!-- /react-text --><!-- react-text: 35789 --> <!-- /react-text --><!-- react-text: 35790 -->67.00<!-- /react-text --></span><span class="player-stats__hide">28.05</span><span class="player-stats__show" style="color: rgb(147, 204, 88);">0.00000023</span></div>
</li>
<li style="margin-bottom: 0px; opacity: 1;">
<div style="width: 100%; display: flex;"><span class="player-stats__show" style="text-decoration: underline; cursor: pointer;">20,633,898,624</span><span class="player-stats__show" style="text-decoration: underline; cursor: pointer;">rikhisahu</span><span class="player-stats__hide">21:37</span><span class="player-stats__hide">0.00000025</span><span class="player-stats__hide"><!-- react-text: 35769 -->1.48<!-- /react-text --><!-- react-text: 35770 -->x<!-- /react-text --></span><span class="player-stats__hide"><!-- react-text: 35772 --><<!-- /react-text --><!-- react-text: 35773 --> <!-- /react-text --><!-- react-text: 35774 -->67.00<!-- /react-text --></span><span class="player-stats__hide">98.12</span><span class="player-stats__show" style="color: rgb(255, 117, 145);">-0.00000025</span></div>
</li>
<li style="margin-bottom: 0px; opacity: 1;">
<div style="width: 100%; display: flex;"><span class="player-stats__show" style="text-decoration: underline; cursor: pointer;">20,633,874,142</span><span class="player-stats__show" style="text-decoration: underline; cursor: pointer;">rikhisahu</span><span class="player-stats__hide">21:36</span><span class="player-stats__hide">0.00000050</span><span class="player-stats__hide"><!-- react-text: 35753 -->1.48<!-- /react-text --><!-- react-text: 35754 -->x<!-- /react-text --></span><span class="player-stats__hide"><!-- react-text: 35756 --><<!-- /react-text --><!-- react-text: 35757 --> <!-- /react-text --><!-- react-text: 35758 -->67.00<!-- /react-text --></span><span class="player-stats__hide">56.81</span><span class="player-stats__show" style="color: rgb(147, 204, 88);">0.00000023</span></div>
</li>
<li style="margin-bottom: 0px; opacity: 1;">
<div style="width: 100%; display: flex;"><span class="player-stats__show" style="text-decoration: underline; cursor: pointer;">20,633,872,421</span><span class="player-stats__show" style="text-decoration: underline; cursor: pointer;">rikhisahu</span><span class="player-stats__hide">21:36</span><span class="player-stats__hide">0.00000025</span><span class="player-stats__hide"><!-- react-text: 35737 -->1.48<!-- /react-text --><!-- react-text: 35738 -->x<!-- /react-text --></span><span class="player-stats__hide"><!-- react-text: 35740 --><<!-- /react-text --><!-- react-text: 35741 --> <!-- /react-text --><!-- react-text: 35742 -->67.00<!-- /react-text --></span><span class="player-stats__hide">99.60</span><span class="player-stats__show" style="color: rgb(255, 117, 145);">-0.00000025</span></div>
</li>
<li style="margin-bottom: 0px; opacity: 1;">
<div style="width: 100%; display: flex;"><span class="player-stats__show" style="text-decoration: underline; cursor: pointer;">20,633,871,002</span><span class="player-stats__show" style="text-decoration: underline; cursor: pointer;">rikhisahu</span><span class="player-stats__hide">21:36</span><span class="player-stats__hide">0.00000025</span><span class="player-stats__hide"><!-- react-text: 35721 -->1.48<!-- /react-text --><!-- react-text: 35722 -->x<!-- /react-text --></span><span class="player-stats__hide"><!-- react-text: 35724 --><<!-- /react-text --><!-- react-text: 35725 --> <!-- /react-text --><!-- react-text: 35726 -->67.00<!-- /react-text --></span><span class="player-stats__hide">6.45</span><span class="player-stats__show" style="color: rgb(147, 204, 88);">0.00000011</span></div>
</li>
</div>
</ul>
</div>
getting below error
Upvotes: 1
Views: 1055
Reputation: 655
Used firefox add-on "FirePath" for tracking right xpath and below is working code,
String lastdeal = driver.findElement(By.xpath(".//*[@id='root']/div/div/div[4]/div/div/div[2]/div/ul[2]/div/li[1]/div/span[8]")).getAttribute("style");
BTW thank you all for your response...... /\
Upvotes: 0
Reputation: 193108
To retrieve the style
attribute from the first <li>
tag's last <span>
tag you can use the following line of code :
String lastdeal = driver.findElement(By.xpath(".//div[@class='container']/ul[@class='index__game-stats__table']/div/li/div/span[last()]")).getAttribute("style");
Upvotes: 1
Reputation: 28
try keeping () in xpath whenever you are trying to find the nth element
(//div[@class='container']/ul[2]/div/li[1]/div/span)[7]
Upvotes: 1
Reputation: 2115
First get all li elements and keep them on a WebElement list. Then find the span element and retrieve the style attribute. You can use like below:
List<WebElement> all_li_Elements = driver.findElements(By.cssSelector("div.container > ul.index__game-stats__table > div > li"));
for (WebElement elem : all_li_Elements) {
String lastdeal = elem.findElement(By.cssSelector("div > span:nth-child(8)")).getAttribute("style");
//System.out.println(lastdeal);
}
You can easily get last span's style value from first and second li inside the for loop.
Upvotes: 1
Reputation: 111591
This XPath,
//div[@class='container']/ul[2]/div/li[1]/div/span[7]
will select the span
containing 31.80
. Adjust the indices if it's another you seek to select.
Update to address question update:
I am trying to get last span's style value from first and second li, using this code
Then adjust the above XPath via position()
and last()
to meet your newly specified criteria:
//div[@class='container']/ul[2]/div/li[position()=1 or position()=2]/div/span[last()]/@style
returns
color: rgb(147, 204, 88);
color: rgb(147, 204, 88);
adjust li[position()=1 or position()=2]
to just li[1]
if you, say, only want the first li
.
Upvotes: 0
Reputation: 5667
Mentioning index for ul in your locator making problem, I guess.
Use Below locators
xpath
By.xpath(".//*[contains(@class,'container')]//ul/div/li/div/span[7]")
cssSelector
By.cssSelector("div.container ul li span:nth-child(7)")
Upvotes: 2