【问题标题】:Find buttons (onclick) and click all of them查找按钮(onclick)并单击所有按钮
【发布时间】:2016-01-27 18:15:36
【问题描述】:

我有麻烦。我使用java和webdriver。我想去http://demo.opencart.com/,找到“ipod”, 我有 4 个结果,我想比较它们(通过单击每个按钮下的“比较”),但有 2 个问题。

  1. 如何点击比较(有onclick)

    <button type="button" data-toggle="tooltip" title="" onclick="compare.add('48');" data-original-title="Compare this Product"><i class="fa fa-exchange"></i></button>

  2. 如何比较我所有的搜索结果?

【问题讨论】:

  • 你有代码吗?你试过什么吗?
  • 您当然可以使用java后端将结果保存在变量中并进行比较。此外,如果您单击显示给我们的按钮,您将调用 compare js 方法
  • 但是,怎么点击呢?我觉得我不能使用 css 或 xpath,因为有 4 个搜索结果,我必须点击所有这些结果
  • 通过“比较”,您肯定想要断言和验证结果(在代码中),而不是仅仅点击一个可以工作/不工作/给出错误结果的按钮......?

标签: javascript java selenium selenium-webdriver


【解决方案1】:

你可以试试这个:

    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();

        }
    }

【讨论】:

  • 我知道这只是演示代码,但所有那些硬编码的等待都是绝对的杀手 - 特别是如果新手复制并粘贴此代码作为示例。
【解决方案2】:

我是这样弄的:

 public void count(){

        List <WebElement> button = driver.findElements(By.xpath("//*[@data-original-title='Compare this Product']"));

    for(WebElement buttons: button){
        buttons.click();
    }

这是一个好的解决方案吗?肯定它比 Kumar 的解决方案短得多,而且它有效

【讨论】:

  • 这部分我应该断言什么?找工作是一项任务,所以我想做出最好的解决方案
【解决方案3】:
List<WebElement> button = driver.findElements(By.xpath("//*[@data-original-title='Compare this Product']"));

以下两个代码的工作方式相同,要么使用 这个:

for(WebElement buttons: button){
    buttons.click();
}

或者使用这个:

for(int i = 0 ; i < button.size() ; i++)
{
    button.get(i).click();
}

【讨论】:

    猜你喜欢
    • 2017-08-30
    • 2021-04-10
    • 1970-01-01
    • 1970-01-01
    • 2022-12-18
    • 1970-01-01
    • 2012-01-22
    • 2022-12-20
    • 1970-01-01
    相关资源
    最近更新 更多