在硒脚本中 使用 对下拉列表进行排序
new Select(driver.findElement(By.cssSelector("select[title="Sort By"]"))).selectByVisibleText("Name");
任何人都可以解释我上述声明cssSelector("select[title="Sort By"]"
的这一部分。 谢谢!
cssSelector("select[title="Sort By"]")
这是定位 Web 元素/元素的技术之一。
你一定听说过 xpath,它是在网页中查找元素的方法之一。
此外,select
是HTML中的标签。title
是属性,排序依据是属性的值。
就像这样:
网页:
<select id="sel" class="drop-down" title="Sort By">
<options>..</options>
<options>..</options>
<options>..</options>
</select>
现在如果非要写cssSelector,可以这样写:
tagname[attribute="attribute value"]
select[id="sel"]
或
select[class="drop-dwon"]
或
select[title="Sort By"]
希望这会有所帮助!
new Select(driver.findElement(By.cssSelector("select[title="Sort By"]"))).selectByVisibleText("Name");
您正在通过 CSS 选择器 https://www.w3schools.com/cssref/css_selectors.asp 进行选择。另一种选择是XPath,它更强大,但更难学习。
这部分By.cssSelector("select[title="Sort By"]")
做的是选择title
属性设置为等于"排序依据"的所有select
元素。尽管通过前缀driver.findElement(
您只请求一个元素,即第一个元素。至少如果是python,Java可能会有所不同,但不在您的问题或标签中。