如何通过 "class" 和 "name" 之外的属性直接查找 WebElements(例如 "title")
How to directly find WebElements by their attributes except "class" and "name" (for example "title")
我是 Java 和 Selenium 的新手,所以如果我的问题听起来有点初级,我提前道歉。
我使用:
driverChrome.findElements(By.className("blabla"));
查找类名为"blabla"的元素,例如:
<span class="blabla" title="the title">...</span>
现在,如果我想通过其他属性查找所有元素怎么办?类似于:
driverChrome.findElements(By.titleValue("the title"));
这是我目前用于执行此任务的代码:
List<WebElement> spans = driverChrome.findElements(By.tagName("span"));
for (WebElement we : spans) {
if (we.getAttribute("title") != null) {
if (we.getAttribute("title").equals("the title")) {
...
break;
}
}
}
但速度不快,使用起来也不方便。
你可以使用 xpath
driverChrome.findElement(By.xpath("//*[@yourAttribute='value']"));
例如:
driverChrome.findElement(By.xpath("//*[@title='the title']"));
您也可以使用 CSS 定位器实现此目的。
By.cssSelector ("span[title='the title']")
CSS 据说选择器更快。
我们在 Selenium WebDriver in Practice 中介绍了一些定位技术:http://selenium-webdriver-in-practice.github.io
您有多种选择。
- 通过 css 选择器
- 通过 xpath
- 按 ID
- 按标签
为了便于阅读教程,请按照下面的links:
http://www.w3schools.com/xsl/xpath_syntax.asp
http://www.w3schools.com/cssref/css_selectors.asp
以上都是使用元素属性,或者,您可以使用元素之间的关系。比如sibling,parents和children。
请参阅这个很棒的 link 了解更多详情。 http://scraping.pro/res/xpath-cheat/xpath_css_dom_recipes.pdf
也有很好的支持和建立的框架供您使用。比如robot framework,它会大大简化你的代码。
http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html
通过XPath
归档元素有很多方法
1 绝对路径
<html>
<body>
<div>
<form>
<input id="demo"/>
</form>
</div>
</body>
<html>
获取'input'标签
xpath="/html/body/div/form/input"
2相对路径
<html>
<body>
<div>
<form>
<input id="demo"/>
</form>
</div>
</body>
<html>
获取'input'标签
xpath="//input"
3 指数
<html>
<body>
<div>
<form>
<input id="demo1"/>
<input id="demo2">
</form>
</div>
</body>
<html>
获取输入'demo2'
xpath="//输入[1]"
4个任意单属性
<html>
<body>
<div>
<form>
<input id="demo1"/>
<input id="demo2" foo="bar">
</form>
</div>
</body>
<html>
获取输入'demo2'
xpath="//input[@id='demo2']" (equivalent to By.id)
或
xpath="//input[@foo='bar']"
5个任意多个属性
<html>
<body>
<div>
<form>
<input id="1" type="submit" />
<input id="2" foo="bar"/>
<input id="3" type="submit" foo="bar"/>
</form>
</div>
</body>
<html>
获取第 3 个输入
xpath="//input[@type='submit'][@foo='bar']"
或
xpath="//input[@type='submit' and @foo='bar']"
如果在这里使用 xpath="//input[@type='submit' 或 @foo='bar']" 你会得到一个数组。您可以通过 driver.findElements(By.xpath(xpath)) (java) 获取列表。否则你会得到第一个元素(如果你只使用 driver.findElement)。因为所有 3 个输入元素都满足您的条件 'or' 并且它给您第一个。
6 包含属性
<html>
<body>
<div>
<form>
<input id="1" type="submit" />
<input id="2" foo="bar" daddy="dog"/>
<input id="3" type="submit" foo="bar"/>
</form>
</div>
</body>
<html>
获取第二个输入
xpath="//input[@daddy]"
因为只有第二个有属性'daddy'
7 内心求索
<html>
<body>
<div>
<form>
<input id="input1" daddy="dog" />
<input id="input2" daddy="pig"/>
</form>
</div>
<div>
<form>
<input id="input3" daddy="dog" />
<input id="input4" daddy="apple"/>
</form>
</div>
</body>
<html>
获得第二个div
xpath="//div[.//input[@daddy='dog'] and .//input[@daddy='apple']]"
总的来说,我现在能解决的就是这些了。希望对你有帮助。
WebElement element = driver.findElement(By.xpath(".//*[@id='ctl00_PLSMainContent_AssistTaskList_assistListView']/div/table/tbody/tr/td[7]/span"));
System.out.println("get attribute is --> " + element.getAttribute("Title"));
我是 Java 和 Selenium 的新手,所以如果我的问题听起来有点初级,我提前道歉。
我使用:
driverChrome.findElements(By.className("blabla"));
查找类名为"blabla"的元素,例如:
<span class="blabla" title="the title">...</span>
现在,如果我想通过其他属性查找所有元素怎么办?类似于:
driverChrome.findElements(By.titleValue("the title"));
这是我目前用于执行此任务的代码:
List<WebElement> spans = driverChrome.findElements(By.tagName("span"));
for (WebElement we : spans) {
if (we.getAttribute("title") != null) {
if (we.getAttribute("title").equals("the title")) {
...
break;
}
}
}
但速度不快,使用起来也不方便。
你可以使用 xpath
driverChrome.findElement(By.xpath("//*[@yourAttribute='value']"));
例如:
driverChrome.findElement(By.xpath("//*[@title='the title']"));
您也可以使用 CSS 定位器实现此目的。
By.cssSelector ("span[title='the title']")
CSS 据说选择器更快。
我们在 Selenium WebDriver in Practice 中介绍了一些定位技术:http://selenium-webdriver-in-practice.github.io
您有多种选择。
- 通过 css 选择器
- 通过 xpath
- 按 ID
- 按标签 为了便于阅读教程,请按照下面的links: http://www.w3schools.com/xsl/xpath_syntax.asp http://www.w3schools.com/cssref/css_selectors.asp
以上都是使用元素属性,或者,您可以使用元素之间的关系。比如sibling,parents和children。
请参阅这个很棒的 link 了解更多详情。 http://scraping.pro/res/xpath-cheat/xpath_css_dom_recipes.pdf
也有很好的支持和建立的框架供您使用。比如robot framework,它会大大简化你的代码。
http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html
通过XPath
归档元素有很多方法1 绝对路径
<html>
<body>
<div>
<form>
<input id="demo"/>
</form>
</div>
</body>
<html>
获取'input'标签
xpath="/html/body/div/form/input"
2相对路径
<html>
<body>
<div>
<form>
<input id="demo"/>
</form>
</div>
</body>
<html>
获取'input'标签
xpath="//input"
3 指数
<html>
<body>
<div>
<form>
<input id="demo1"/>
<input id="demo2">
</form>
</div>
</body>
<html>
获取输入'demo2'
xpath="//输入[1]"
4个任意单属性
<html>
<body>
<div>
<form>
<input id="demo1"/>
<input id="demo2" foo="bar">
</form>
</div>
</body>
<html>
获取输入'demo2'
xpath="//input[@id='demo2']" (equivalent to By.id)
或
xpath="//input[@foo='bar']"
5个任意多个属性
<html>
<body>
<div>
<form>
<input id="1" type="submit" />
<input id="2" foo="bar"/>
<input id="3" type="submit" foo="bar"/>
</form>
</div>
</body>
<html>
获取第 3 个输入
xpath="//input[@type='submit'][@foo='bar']"
或
xpath="//input[@type='submit' and @foo='bar']"
如果在这里使用 xpath="//input[@type='submit' 或 @foo='bar']" 你会得到一个数组。您可以通过 driver.findElements(By.xpath(xpath)) (java) 获取列表。否则你会得到第一个元素(如果你只使用 driver.findElement)。因为所有 3 个输入元素都满足您的条件 'or' 并且它给您第一个。
6 包含属性
<html>
<body>
<div>
<form>
<input id="1" type="submit" />
<input id="2" foo="bar" daddy="dog"/>
<input id="3" type="submit" foo="bar"/>
</form>
</div>
</body>
<html>
获取第二个输入
xpath="//input[@daddy]"
因为只有第二个有属性'daddy'
7 内心求索
<html>
<body>
<div>
<form>
<input id="input1" daddy="dog" />
<input id="input2" daddy="pig"/>
</form>
</div>
<div>
<form>
<input id="input3" daddy="dog" />
<input id="input4" daddy="apple"/>
</form>
</div>
</body>
<html>
获得第二个div
xpath="//div[.//input[@daddy='dog'] and .//input[@daddy='apple']]"
总的来说,我现在能解决的就是这些了。希望对你有帮助。
WebElement element = driver.findElement(By.xpath(".//*[@id='ctl00_PLSMainContent_AssistTaskList_assistListView']/div/table/tbody/tr/td[7]/span"));
System.out.println("get attribute is --> " + element.getAttribute("Title"));