Reputation: 63
i have trouble. Im using java and webdriver. I want to go http://demo.opencart.com/, find "ipod", I have 4 results, and I want to compare them (by clicking under each button "compare"), but there are 2 problem.
how to click on compare (there are onclick)
<button type="button" data-toggle="tooltip" title="" onclick="compare.add('48');" data-original-title="Compare this Product"><i class="fa fa-exchange"></i></butto
n>
how to compare all my search results?
Upvotes: 0
Views: 1558
Reputation: 328
List<WebElement> button = driver.findElements(By.xpath("//*[@data-original-title='Compare this Product']"));
both of the below code works same way, either use this :
for(WebElement buttons: button){
buttons.click();
}
or use this :
for(int i = 0 ; i < button.size() ; i++)
{
button.get(i).click();
}
Upvotes: 0
Reputation: 63
I made sth like this:
public void count(){
List <WebElement> button = driver.findElements(By.xpath("//*[@data-original-title='Compare this Product']"));
for(WebElement buttons: button){
buttons.click();
}
Is it a good solution? For sure its much shorter than Kumar's solution, and it works
Upvotes: 0
Reputation: 328
You can try this:
package pkYourPackage;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
// http://demo.opencart.com/index.php?route=product/category&path=24
public class OpenCart_StackOverflow {
static WebDriver driver;
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:/.....put_ path/chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://demo.opencart.com/index.php?route=product/category&path=24");
try{Thread.sleep(5000);}catch(Exception e){}
List<WebElement> list = driver.findElements(By.xpath("//button[contains(@data-original-title,'Compare this')]/i"));
for(int i = 0 ; i < list.size() ; i++){
list.get(i).click();
try{Thread.sleep(1500);}catch(Exception e){}
}
try{Thread.sleep(1500);}catch(Exception e){}
// click on 'product comparison' to compare
((JavascriptExecutor)driver).executeScript("arguments[0].click();",driver.findElement(By.xpath("//*[contains(text(),'product comparison')]")));
try{Thread.sleep(10000);}catch(Exception e){}
driver.close();
driver.quit();
}
}
Upvotes: 2