2

How can I use selenium webdriver to get all links from the page that have the property a[href*=/simulation/form/] and then open and close each of them?

I tried the following code but it returns TypeError: link.click is not a function

var simLinks = driver.findElements(By.css('a[href*=/simulation/form/]'));
for(var link in simLinks){
 link.click();
 driver.wait(30000);
 driver.back();
}

If I do console.log(simLinks.length) it returns undefined

Instead, it works perfect if I try to open a single link of that type:

var simLinks = driver.findElement(By.css('a[href*=/simulation/form/]')).click();
asked Jan 12, 2017 at 17:54
3
  • what is the type of simLinks? Commented Jan 12, 2017 at 17:57
  • simLinks should contain all elements with property 'a[href*=/simulation/form/]... Commented Jan 12, 2017 at 17:59
  • I see that in Java it would be done like List<WebElement> simLinks = driver.findElements(By.css('a[href*=/simulation/form/]'));, but I'm using javascript. Commented Jan 12, 2017 at 18:04

2 Answers 2

2

Use the util.inspect() function to inspect objects:

const util = require('util');
...
console.log(util.inspect(simLinks));

Once you do that, you will see that simLinks is a ManagedPromise, so you need to change your code as follows to make it work:

driver.findElements(By.css('a[href*="/simulation/form/"]')).then(function (simLinks) {
 for (let link of simLinks) {
 link.click();
 driver.wait(until.urlContains('/simulation/form/', 30000));
 driver.navigate().back();
 }
});

The problem with that, however, is that only the first click works, but once you navigate back there is a legitimate StaleElementReferenceError - the page has indeed changed and the next element in the loop is no longer attached to the visible page. Therefore, a better strategy would be to collect the link addresses instead and use these with driver.navigate().to(link):

driver.findElements(By.css('a[href*="/simulation/form/"]')).then(function (simLinks) {
 var hrefs = simLinks.map(link => link.getAttribute('href'));
 for (let link of hrefs) {
 driver.navigate().to(link);
 driver.wait(until.urlContains('/simulation/form/', 30000));
 driver.navigate().back();
 }
});
answered Jan 12, 2017 at 18:41
Sign up to request clarification or add additional context in comments.

7 Comments

I still got that error TypeError: link.click is not a function, but the good thing is that the console returns 2 WebElement objects, which are my links
Ah, indeed, simLinks is an array. I fixed the answer accordingly, should work now :).
Thank you, this should do it but now it throws ElementNotVisibleError. How comes that the element is not visible since it gets logged in the console?
FYI I also tried to get the links as follows By.css('a[data-ga-name="Property Summary: Edit Simulation"]'. This is how a link looks on the page: <a href="/simulation/form/15760?p=7747&amp;t=3" data-ga-name="Property Summary: Edit Simulation">
I tried the example with Google and the first click works. But once you navigate back there is a legitimate StaleElementReferenceError - the page has indeed changed and the next element in the loop is no longer attached to the visible page. I'll think a bit how to deal with this...
|
0

I believe you should be using:

console.log(simLinks.size())

I don't know if the () is required in javascript - it is in java.

answered Jan 12, 2017 at 18:30

1 Comment

I'm using javascript, not java

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.