How to find an Element by index in selenium webdriver for javaHow to find Element [For Selenium]List.get(index) to extend xpath for another List <WebElement>How do I efficiently iterate over each entry in a Java Map?How do I call one constructor from another in Java?How do I read / convert an InputStream into a String in Java?How do I generate random integers within a specific range in Java?How to get an enum value from a string value in Java?How do I determine whether an array contains a particular value in Java?How do I declare and initialize an array in Java?How to take screenshot with Selenium WebDriverHow to split a string in JavaHow do I convert a String to an int in Java?
Multi tool use
When is one 'Ready' to make Original Contributions to Mathematics?
What is it called when the tritone is added to a minor scale?
How to reclaim personal item I've lent to the office without burning bridges?
Boss furious on bad appraisal
What's the big deal about the Nazgûl losing their horses?
Isn't "Dave's protocol" good if only the database, and not the code, is leaked?
How serious is plagiarism in a master’s thesis?
Do I need to be legally qualified to install a Hive smart thermostat?
Why no parachutes in the Orion AA2 abort test?
Was I wrongfully denied boarding for having a Schengen visa issued from the second country on my itinerary?
Can a USB hub be used to access a drive from 2 devices?
Do intermediate subdomains need to exist?
How do I check that users don't write down their passwords?
What do you call the angle of the direction of an airplane?
In the Seventh Seal why does Death let the chess game happen?
Why did Super-VGA offer the 5:4 1280*1024 resolution?
Can you take the Dodge action while prone?
Question about targeting a Hexproof creature
How do I iterate equal values with the standard library?
Should I warn my boss I might take sick leave?
Why do Klingons use cloaking devices?
Would Disciple of Life supercharge Regenerate?
How can a ban from entering the US be lifted?
n-level Ouroboros Quine
How to find an Element by index in selenium webdriver for java
How to find Element [For Selenium]List.get(index) to extend xpath for another List <WebElement>How do I efficiently iterate over each entry in a Java Map?How do I call one constructor from another in Java?How do I read / convert an InputStream into a String in Java?How do I generate random integers within a specific range in Java?How to get an enum value from a string value in Java?How do I determine whether an array contains a particular value in Java?How do I declare and initialize an array in Java?How to take screenshot with Selenium WebDriverHow to split a string in JavaHow do I convert a String to an int in Java?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Im trying to automate the Google Images page:
https://www.google.com/search?q=pluralsight&biw=1416&bih=685&source=lnms&tbm=isch&sa=X&ei=qGd6VN6bEZTooAT7q4C4BQ&sqi=2&ved=0CAgQ_AUoAw
All the images have the same class but no id and the results are constantly changing. So I would like to be able to click on the images based on their index.
I know how to do it in C#...but I cant figure out how to specify in the index in Java. When I try to select an index beyond 0, I get and IndexOutOfBounds error, but i cant figure out why
WebElement image = chromeDriver.findElement(By.className("rg_di"));
WebElement imageLink = image.findElements(By.tagName("a")).get(1);
imageLink.click();
Here is the entire code im using...any help would be appreciated:
System.setProperty("webdriver.chrome.driver", "/Users/user/chromedriver");
WebDriver chromeDriver = new ChromeDriver();
chromeDriver.get("http://www.google.com");
WebElement searchBox = chromeDriver.findElement(By.id("gbqfq"));
searchBox.sendKeys("pluralsight");
searchBox.sendKeys(Keys.RETURN);
chromeDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement imagesLink = chromeDriver.findElement(By.linkText("Images"));
imagesLink.click();
WebElement image = chromeDriver.findElement(By.className("rg_di"));
WebElement imageLink = image.findElements(By.tagName("a")).get(1);
imageLink.click();
Any help would be greatly appreciated
java selenium selenium-webdriver
add a comment |
Im trying to automate the Google Images page:
https://www.google.com/search?q=pluralsight&biw=1416&bih=685&source=lnms&tbm=isch&sa=X&ei=qGd6VN6bEZTooAT7q4C4BQ&sqi=2&ved=0CAgQ_AUoAw
All the images have the same class but no id and the results are constantly changing. So I would like to be able to click on the images based on their index.
I know how to do it in C#...but I cant figure out how to specify in the index in Java. When I try to select an index beyond 0, I get and IndexOutOfBounds error, but i cant figure out why
WebElement image = chromeDriver.findElement(By.className("rg_di"));
WebElement imageLink = image.findElements(By.tagName("a")).get(1);
imageLink.click();
Here is the entire code im using...any help would be appreciated:
System.setProperty("webdriver.chrome.driver", "/Users/user/chromedriver");
WebDriver chromeDriver = new ChromeDriver();
chromeDriver.get("http://www.google.com");
WebElement searchBox = chromeDriver.findElement(By.id("gbqfq"));
searchBox.sendKeys("pluralsight");
searchBox.sendKeys(Keys.RETURN);
chromeDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement imagesLink = chromeDriver.findElement(By.linkText("Images"));
imagesLink.click();
WebElement image = chromeDriver.findElement(By.className("rg_di"));
WebElement imageLink = image.findElements(By.tagName("a")).get(1);
imageLink.click();
Any help would be greatly appreciated
java selenium selenium-webdriver
add a comment |
Im trying to automate the Google Images page:
https://www.google.com/search?q=pluralsight&biw=1416&bih=685&source=lnms&tbm=isch&sa=X&ei=qGd6VN6bEZTooAT7q4C4BQ&sqi=2&ved=0CAgQ_AUoAw
All the images have the same class but no id and the results are constantly changing. So I would like to be able to click on the images based on their index.
I know how to do it in C#...but I cant figure out how to specify in the index in Java. When I try to select an index beyond 0, I get and IndexOutOfBounds error, but i cant figure out why
WebElement image = chromeDriver.findElement(By.className("rg_di"));
WebElement imageLink = image.findElements(By.tagName("a")).get(1);
imageLink.click();
Here is the entire code im using...any help would be appreciated:
System.setProperty("webdriver.chrome.driver", "/Users/user/chromedriver");
WebDriver chromeDriver = new ChromeDriver();
chromeDriver.get("http://www.google.com");
WebElement searchBox = chromeDriver.findElement(By.id("gbqfq"));
searchBox.sendKeys("pluralsight");
searchBox.sendKeys(Keys.RETURN);
chromeDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement imagesLink = chromeDriver.findElement(By.linkText("Images"));
imagesLink.click();
WebElement image = chromeDriver.findElement(By.className("rg_di"));
WebElement imageLink = image.findElements(By.tagName("a")).get(1);
imageLink.click();
Any help would be greatly appreciated
java selenium selenium-webdriver
Im trying to automate the Google Images page:
https://www.google.com/search?q=pluralsight&biw=1416&bih=685&source=lnms&tbm=isch&sa=X&ei=qGd6VN6bEZTooAT7q4C4BQ&sqi=2&ved=0CAgQ_AUoAw
All the images have the same class but no id and the results are constantly changing. So I would like to be able to click on the images based on their index.
I know how to do it in C#...but I cant figure out how to specify in the index in Java. When I try to select an index beyond 0, I get and IndexOutOfBounds error, but i cant figure out why
WebElement image = chromeDriver.findElement(By.className("rg_di"));
WebElement imageLink = image.findElements(By.tagName("a")).get(1);
imageLink.click();
Here is the entire code im using...any help would be appreciated:
System.setProperty("webdriver.chrome.driver", "/Users/user/chromedriver");
WebDriver chromeDriver = new ChromeDriver();
chromeDriver.get("http://www.google.com");
WebElement searchBox = chromeDriver.findElement(By.id("gbqfq"));
searchBox.sendKeys("pluralsight");
searchBox.sendKeys(Keys.RETURN);
chromeDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement imagesLink = chromeDriver.findElement(By.linkText("Images"));
imagesLink.click();
WebElement image = chromeDriver.findElement(By.className("rg_di"));
WebElement imageLink = image.findElements(By.tagName("a")).get(1);
imageLink.click();
Any help would be greatly appreciated
java selenium selenium-webdriver
java selenium selenium-webdriver
asked Nov 30 '14 at 0:52
Tim BolandTim Boland
1,1683 gold badges18 silver badges35 bronze badges
1,1683 gold badges18 silver badges35 bronze badges
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
In your code:
WebElement image = chromeDriver.findElement(By.className("rg_di"));
will return the first element found on the page with a class of "rg_di".
That element has only one <a href=... /a>
tag in it.
You are getting an IndexOutOfBounds exception because you are asking for the second one (zero based indexing). If you change your final WebElement to:
WebElement imageLink = image.findElements(By.tagName("a")).get(0);
The code should work for you with that small change.
This is my quick version (note the lack of storing elements I only need to do one thing with as WebElements):
public static void main(String[] args)
// I don't have Chrome installed >.<
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.google.com");
WebElement searchBox = driver.findElement(By.id("gbqfq"));
searchBox.sendKeys("pluralsight");
searchBox.sendKeys(Keys.RETURN);
driver.findElement(By.linkText("Images")).click();
WebElement image = driver.findElement(By.className("rg_di"));
image.findElements(By.tagName("a")).get(0).click();
// super-shortened version:
// driver.findElement(By.className("rg_di")).findElements(By.tagName("a")).get(0).click();
Yes i updated the code to use the id of the ordered list that all of the image links are in...so now i can get to each of them by index
– Tim Boland
Nov 30 '14 at 5:04
add a comment |
I'd do:
List<WebElement> we = chromeDriver.findElements(By.cssSelector(".your-class a"));
we.get(1) //should get first element in array
add a comment |
This code worked very well when we have similar object properties for same web buttons, then using
List<WebElement> we = webdriver.findElements(By.cssSelector(""));
and then getting
we.get(1).click();
Thank you so much for posting this answer.
add a comment |
Another solution may be like this:
If the class name and the index of the web element are known, then following code works:
driver.findElement(By.xpath("(//*[@class='android.widget.ImageView'])[18]")).click();
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f27208872%2fhow-to-find-an-element-by-index-in-selenium-webdriver-for-java%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
In your code:
WebElement image = chromeDriver.findElement(By.className("rg_di"));
will return the first element found on the page with a class of "rg_di".
That element has only one <a href=... /a>
tag in it.
You are getting an IndexOutOfBounds exception because you are asking for the second one (zero based indexing). If you change your final WebElement to:
WebElement imageLink = image.findElements(By.tagName("a")).get(0);
The code should work for you with that small change.
This is my quick version (note the lack of storing elements I only need to do one thing with as WebElements):
public static void main(String[] args)
// I don't have Chrome installed >.<
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.google.com");
WebElement searchBox = driver.findElement(By.id("gbqfq"));
searchBox.sendKeys("pluralsight");
searchBox.sendKeys(Keys.RETURN);
driver.findElement(By.linkText("Images")).click();
WebElement image = driver.findElement(By.className("rg_di"));
image.findElements(By.tagName("a")).get(0).click();
// super-shortened version:
// driver.findElement(By.className("rg_di")).findElements(By.tagName("a")).get(0).click();
Yes i updated the code to use the id of the ordered list that all of the image links are in...so now i can get to each of them by index
– Tim Boland
Nov 30 '14 at 5:04
add a comment |
In your code:
WebElement image = chromeDriver.findElement(By.className("rg_di"));
will return the first element found on the page with a class of "rg_di".
That element has only one <a href=... /a>
tag in it.
You are getting an IndexOutOfBounds exception because you are asking for the second one (zero based indexing). If you change your final WebElement to:
WebElement imageLink = image.findElements(By.tagName("a")).get(0);
The code should work for you with that small change.
This is my quick version (note the lack of storing elements I only need to do one thing with as WebElements):
public static void main(String[] args)
// I don't have Chrome installed >.<
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.google.com");
WebElement searchBox = driver.findElement(By.id("gbqfq"));
searchBox.sendKeys("pluralsight");
searchBox.sendKeys(Keys.RETURN);
driver.findElement(By.linkText("Images")).click();
WebElement image = driver.findElement(By.className("rg_di"));
image.findElements(By.tagName("a")).get(0).click();
// super-shortened version:
// driver.findElement(By.className("rg_di")).findElements(By.tagName("a")).get(0).click();
Yes i updated the code to use the id of the ordered list that all of the image links are in...so now i can get to each of them by index
– Tim Boland
Nov 30 '14 at 5:04
add a comment |
In your code:
WebElement image = chromeDriver.findElement(By.className("rg_di"));
will return the first element found on the page with a class of "rg_di".
That element has only one <a href=... /a>
tag in it.
You are getting an IndexOutOfBounds exception because you are asking for the second one (zero based indexing). If you change your final WebElement to:
WebElement imageLink = image.findElements(By.tagName("a")).get(0);
The code should work for you with that small change.
This is my quick version (note the lack of storing elements I only need to do one thing with as WebElements):
public static void main(String[] args)
// I don't have Chrome installed >.<
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.google.com");
WebElement searchBox = driver.findElement(By.id("gbqfq"));
searchBox.sendKeys("pluralsight");
searchBox.sendKeys(Keys.RETURN);
driver.findElement(By.linkText("Images")).click();
WebElement image = driver.findElement(By.className("rg_di"));
image.findElements(By.tagName("a")).get(0).click();
// super-shortened version:
// driver.findElement(By.className("rg_di")).findElements(By.tagName("a")).get(0).click();
In your code:
WebElement image = chromeDriver.findElement(By.className("rg_di"));
will return the first element found on the page with a class of "rg_di".
That element has only one <a href=... /a>
tag in it.
You are getting an IndexOutOfBounds exception because you are asking for the second one (zero based indexing). If you change your final WebElement to:
WebElement imageLink = image.findElements(By.tagName("a")).get(0);
The code should work for you with that small change.
This is my quick version (note the lack of storing elements I only need to do one thing with as WebElements):
public static void main(String[] args)
// I don't have Chrome installed >.<
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.google.com");
WebElement searchBox = driver.findElement(By.id("gbqfq"));
searchBox.sendKeys("pluralsight");
searchBox.sendKeys(Keys.RETURN);
driver.findElement(By.linkText("Images")).click();
WebElement image = driver.findElement(By.className("rg_di"));
image.findElements(By.tagName("a")).get(0).click();
// super-shortened version:
// driver.findElement(By.className("rg_di")).findElements(By.tagName("a")).get(0).click();
answered Nov 30 '14 at 4:45
cbarrerascbarreras
954 bronze badges
954 bronze badges
Yes i updated the code to use the id of the ordered list that all of the image links are in...so now i can get to each of them by index
– Tim Boland
Nov 30 '14 at 5:04
add a comment |
Yes i updated the code to use the id of the ordered list that all of the image links are in...so now i can get to each of them by index
– Tim Boland
Nov 30 '14 at 5:04
Yes i updated the code to use the id of the ordered list that all of the image links are in...so now i can get to each of them by index
– Tim Boland
Nov 30 '14 at 5:04
Yes i updated the code to use the id of the ordered list that all of the image links are in...so now i can get to each of them by index
– Tim Boland
Nov 30 '14 at 5:04
add a comment |
I'd do:
List<WebElement> we = chromeDriver.findElements(By.cssSelector(".your-class a"));
we.get(1) //should get first element in array
add a comment |
I'd do:
List<WebElement> we = chromeDriver.findElements(By.cssSelector(".your-class a"));
we.get(1) //should get first element in array
add a comment |
I'd do:
List<WebElement> we = chromeDriver.findElements(By.cssSelector(".your-class a"));
we.get(1) //should get first element in array
I'd do:
List<WebElement> we = chromeDriver.findElements(By.cssSelector(".your-class a"));
we.get(1) //should get first element in array
answered Nov 30 '14 at 2:11
bcarbcar
6647 silver badges19 bronze badges
6647 silver badges19 bronze badges
add a comment |
add a comment |
This code worked very well when we have similar object properties for same web buttons, then using
List<WebElement> we = webdriver.findElements(By.cssSelector(""));
and then getting
we.get(1).click();
Thank you so much for posting this answer.
add a comment |
This code worked very well when we have similar object properties for same web buttons, then using
List<WebElement> we = webdriver.findElements(By.cssSelector(""));
and then getting
we.get(1).click();
Thank you so much for posting this answer.
add a comment |
This code worked very well when we have similar object properties for same web buttons, then using
List<WebElement> we = webdriver.findElements(By.cssSelector(""));
and then getting
we.get(1).click();
Thank you so much for posting this answer.
This code worked very well when we have similar object properties for same web buttons, then using
List<WebElement> we = webdriver.findElements(By.cssSelector(""));
and then getting
we.get(1).click();
Thank you so much for posting this answer.
edited Mar 18 '16 at 2:39
BMW
24.7k5 gold badges60 silver badges75 bronze badges
24.7k5 gold badges60 silver badges75 bronze badges
answered Mar 18 '16 at 2:37
Pardhiva EPardhiva E
211 bronze badge
211 bronze badge
add a comment |
add a comment |
Another solution may be like this:
If the class name and the index of the web element are known, then following code works:
driver.findElement(By.xpath("(//*[@class='android.widget.ImageView'])[18]")).click();
add a comment |
Another solution may be like this:
If the class name and the index of the web element are known, then following code works:
driver.findElement(By.xpath("(//*[@class='android.widget.ImageView'])[18]")).click();
add a comment |
Another solution may be like this:
If the class name and the index of the web element are known, then following code works:
driver.findElement(By.xpath("(//*[@class='android.widget.ImageView'])[18]")).click();
Another solution may be like this:
If the class name and the index of the web element are known, then following code works:
driver.findElement(By.xpath("(//*[@class='android.widget.ImageView'])[18]")).click();
edited Mar 25 at 19:54
trincot
138k18 gold badges105 silver badges143 bronze badges
138k18 gold badges105 silver badges143 bronze badges
answered Mar 25 at 19:33
Beytullah uzunBeytullah uzun
1
1
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f27208872%2fhow-to-find-an-element-by-index-in-selenium-webdriver-for-java%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
sqIokNEASf,gdqbTzmXw7,m1ivTqKxW,EEebAQdd,D epH