问题
I am very new at Java and Selenium so my apologies in advance if my question sounds a bit primary.
I use:
driverChrome.findElements(By.className("blabla"));
to find elements which have "blabla" as their className, for example:
<span class="blabla" title="the title">...</span>
Now, what if I want to find all elements by their other attributes? something like:
driverChrome.findElements(By.titleValue("the title"));
This is the code that I am currently using to do this task:
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;
}
}
}
but it is not fast and easy to use.
回答1:
There are many methods while archiving an element by XPath
1 Absolutely path
<html>
<body>
<div>
<form>
<input id="demo"/>
</form>
</div>
</body>
<html>
To get the 'input' tag
xpath="/html/body/div/form/input"
2 Relative path
<html>
<body>
<div>
<form>
<input id="demo"/>
</form>
</div>
</body>
<html>
To get the 'input' tag
xpath="//input"
3 Index
<html>
<body>
<div>
<form>
<input id="demo1"/>
<input id="demo2">
</form>
</div>
</body>
<html>
To get the input 'demo2'
xpath="//input[1]"
4 Arbitrary single attribute
<html>
<body>
<div>
<form>
<input id="demo1"/>
<input id="demo2" foo="bar">
</form>
</div>
</body>
<html>
To get input 'demo2'
xpath="//input[@id='demo2']" (equivalent to By.id)
Or
xpath="//input[@foo='bar']"
5 Arbitrary multiple attributes
<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>
To get 3rd input
xpath="//input[@type='submit'][@foo='bar']"
Or
xpath="//input[@type='submit' and @foo='bar']"
If use xpath="//input[@type='submit' or @foo='bar']" here you'll get an array. You can get the List by driver.findElements(By.xpath(xpath)) (java). Otherwise you'll get the first element(If you just use driver.findElement). Because all of the 3 input elements meet your condition 'or' and it gives you the first one.
6 Contains attribute
<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>
To get the second input
xpath="//input[@daddy]"
Because only the second one has attribute 'daddy'
7 Inner searching
<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>
To get the second div
xpath="//div[.//input[@daddy='dog'] and .//input[@daddy='apple']]"
Overall those are all I can work out for now. Hope it helps.
回答2:
You can do it using xpath
driverChrome.findElement(By.xpath("//*[@yourAttribute='value']"));
For example:
driverChrome.findElement(By.xpath("//*[@title='the title']"));
回答3:
You can also achieve this using CSS locators.
By.cssSelector ("span[title='the title']")
CSS selector are rumoured to be faster.
We cover a number of locating techniques in Selenium WebDriver in Practice: http://selenium-webdriver-in-practice.github.io
回答4:
There a number of options you can go for.
- by css selector
- by xpath
- by id
- by tag For easy reading tutorials, please follow the links below: http://www.w3schools.com/xsl/xpath_syntax.asp http://www.w3schools.com/cssref/css_selectors.asp
All above is using element attributes, alternatively, you can use relationship among elements. Such as sibling, parents and children.
Please refer to this awesome link for more details. http://scraping.pro/res/xpath-cheat/xpath_css_dom_recipes.pdf
There are also very well supported and established frameworks for you to use. For example, robot framework, it will vastly simplify your codes.
http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html
回答5:
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"));
来源:https://stackoverflow.com/questions/32236054/how-to-directly-find-webelements-by-their-attributes-except-class-and-name