我正在尝试使用此代码从第一个和第二个 li 获取最后一个 span 的样式值
String lastdeal = driver.findElement(By.xpath(".//*[contains(@class,'container')]//ul[1]/div/li/div/span[7]")).getAttribute("style");
下面是我的html代码,类容器前有很多div标签。
显示无法定位错误
请让我知道我错过了什么
<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>
低于错误
最佳答案
我想在您的定位器制作问题中提到 ul 的索引。
使用下方定位器
路径
By.xpath(".//*[contains(@class,'container')]//ul/div/li/div/span[7]")
CSS选择器
By.cssSelector("div.container ul li span:nth-child(7)")
关于Java Selenium : Unable to locate element,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47397523/