Selenium Webdriver Java: Help selecting multiple elements with same class name

183 views
Skip to first unread message

fue yang

unread,
Mar 26, 2018, 11:54:13 AM3/26/18
to Selenium Users
Hello, I am a beginner with selenium webdriver and I am trying to compare two items on any certain website, such as newegg.com.
//Clicking and comparing two items
WebElement compare1 = driver.findElement(By.xpath("//span[@class='form-checkbox']"));
WebElement compare2 = driver.findElement(By.xpath("//span[@class='form-checkbox']"));
compare1.click();
Thread.sleep(2000);
compare2.click();
Thread.sleep(2000);


This is part of my current code to compare the two items. I have tried by.className, but it couldn't get more than one. I have also tried using xpath like //input[1] and //input[2] and so on, but still not able to get anything.
If I use the current code, it will only select the first item and only one item. 

I was wondering if I could get some help on being able to select another item to compare it.

Attached is newegg's source code for the example I'm using.

Any pointers, tips, anything would be great to have.

Thanks
compare.PNG

fue yang

unread,
Mar 26, 2018, 12:21:55 PM3/26/18
to Selenium Users
I'm also using it on Google Chrome Driver 2.36. 

fue yang

unread,
Mar 26, 2018, 12:24:52 PM3/26/18
to Selenium Users
I have also tried using it by the input ID, as well as the input value. Also tried using xpath through the //div/div/label/div/input/span and so forth. Those xpaths and input id failed to find the element. So I had to just use the span class to finally be able to click and find the element. 


On Monday, March 26, 2018 at 11:54:13 AM UTC-4, fue yang wrote:

Naresh Chaurasia

unread,
Apr 3, 2018, 4:42:10 AM4/3/18
to Selenium Users
Instead of using findElement, use findElements.

It will give you multiple element to compare.
Reply all
Reply to author
Forward
0 new messages