我目前正在学习 Selenium,并获得了一个要创建的测试用例,使用store.steampowered.com/search/?filter=topsellers作为测试站点。
我需要获取复选框“操作”附近的数字,如在屏幕截图上突出显示。 https://i.stack.imgur.com/hn3PH.png
这是描述此特定复选框的页面代码的一部分:
<div class="tab_filter_control_row " data-param="tags" data-value="19" data-loc="Action" data-clientside="0">
<span class="tab_filter_control tab_filter_control_include " data-param="tags" data-value="19" data-loc="Action" data-clientside="0" data-gpfocus="item">
<span>
<span class="tab_filter_control_checkbox"></span>
<span class="tab_filter_control_label">Action</span>
<span class="tab_filter_control_count" style="">30</span>
</span>
</span>
<span class="tab_filter_control_not " data-param="untags" data-value="19" data-icon="https://store.akamai.steamstatic.com/public/images/search_crouton_not.svg" data-loc="Action" data-clientside="0" data-tooltip-text="Exclude results with this tag" data-gpfocus="item"><img src="https://store.akamai.steamstatic.com/public/images/search_checkbox_not.svg" width="16px" height="16px"></span>
</div>
为此,我编写了这个 XPath 定位器://span[@data-value='19']//*[@class='tab_filter_control_count']。但是当我做这样的事情时:
string str = driver.FindElement(By.XPath("//span[@data-value='19']//*[@class='tab_filter_control_count']"));
由于某种原因,我会得到“1 635”,而不是“30”。我尝试使用不同的 XPath 获取文本,//div[@data-collapse-name='tags']//*[@data-value='19'],但结果将是“操作 1 635”。
也许有人会解释 30 如何变成 1 635 以及我如何得到正确的数字。
提取文本30
标签旁边Action你可以使用以下内容定位策略 https://stackoverflow.com/questions/48369043/official-locator-strategies-for-the-webdriver/48376890#48376890:
理想情况下,你必须诱导WebDriver等待 https://stackoverflow.com/questions/57554298/webdriverwait-is-not-waiting-for-the-element-i-specify/57554488#57554488为了想要的元素可见你可以使用以下内容定位策略 https://stackoverflow.com/questions/48369043/official-locator-strategies-for-the-webdriver/48376890#48376890:
-
Using xpath and GetAttribute()
:
Console.WriteLine(new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementIsVisible(By.XPath("//span[text()='Action']//following::span[1]"))).GetAttribute("innerHTML"));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)