Getting Link Attributes and Tabs Switching
Further explore hyperlinks in this lesson by learning how to get data attributes of a link and how to open it in a new window.
We'll cover the following
Getting link data attributes
Selenium provides functionality not only for driving the link, but also for driving its attributes. To do this, we first need to identify the link. Once it is identified, we can get to its other attributes (this generally applies to most of the web controls). The following script shows how to get data attributes of a link:
driver.findElement(By.linkText("Recommend Selenium")).getAttribute("href").then(
function(the_href) {
assert(the_href.contains("/site/index.html"))
});
driver.findElement(By.linkText("Recommend Selenium")).getAttribute("id").then(function(the_id) {
assert.equal("recommend_selenium_link", the_id)
});
driver.findElement(By.id("recommend_selenium_link")).getText().then(
function(elemtext){
assert.equal("Recommend Selenium", elemtext)
});
driver.findElement(By.id("recommend_selenium_link")).getTagName().then(
function(tagname) {
assert.equal("a", tagname)
});
Furthermore, we can also get the values of custom attributes of an element and its inline CSS by:
driver.findElement(By.id("recommend_selenium_link")).getAttribute("style").then(
function(the_style){
assert.equal("font-size: 14px;", the_style)
});
driver.findElement(By.id("recommend_selenium_link")).getAttribute("data-id").then(
function(the_data_id){
assert.equal("123", the_data_id)
});
Get hands-on with 1400+ tech skills courses.