Reputation: 735
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.
Upvotes: 3
Views: 21632
Reputation: 301
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"));
Upvotes: 0
Reputation: 942
There are many methods while archiving an element by XPath
<html>
<body>
<div>
<form>
<input id="demo"/>
</form>
</div>
</body>
<html>
To get the 'input' tag
xpath="/html/body/div/form/input"
<html>
<body>
<div>
<form>
<input id="demo"/>
</form>
</div>
</body>
<html>
To get the 'input' tag
xpath="//input"
<html>
<body>
<div>
<form>
<input id="demo1"/>
<input id="demo2">
</form>
</div>
</body>
<html>
To get the input 'demo2'
xpath="//input[1]"
<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']"
<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.
<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'
<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.
Upvotes: 7
Reputation: 2149
There a number of options you can go for.
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
Upvotes: 0
Reputation: 1016
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
Upvotes: 0
Reputation: 369
You can do it using xpath
driverChrome.findElement(By.xpath("//*[@yourAttribute='value']"));
For example:
driverChrome.findElement(By.xpath("//*[@title='the title']"));
Upvotes: 0