Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 单击相关按钮_Java_Selenium_Selenium Webdriver - Fatal编程技术网

Java 单击相关按钮

Java 单击相关按钮,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我有一个页面,其中显示了许多产品,我需要找到一种方法来单击所选产品的相关按钮(产品名称来自数据表)。每个产品的详细信息都存储在div class=“cf” 城市7天超级骑士 城市7天超级骑士 无限制旅行X76服务 1名成人 在线购买即可享受优惠 全部的 1张票添加到篮子中。 以下是我如何应对的步骤: 获取产品的名称 检查产品是否在线销售,然后单击相关按钮。找到产品后,我想单击的按钮有:添加到购物篮和关闭图标 最新代码: // List products that have cl

我有一个页面,其中显示了许多产品,我需要找到一种方法来
单击所选产品的相关
按钮
(产品名称来自数据表)。每个产品的详细信息都存储在
div class=“cf”


城市7天超级骑士
城市7天超级骑士
  • 无限制旅行X76服务
  • 1名成人
  • 在线购买即可享受优惠
全部的 1张票添加到篮子中。

以下是我如何应对的步骤:

  • 获取产品的名称
  • 检查产品是否在线销售,然后单击相关按钮。找到产品后,我想单击的按钮有:添加到购物篮和关闭图标
  • 最新代码:

    // List products that have class-cf
    List<WebElement> listProduct = driver.findElements(By.xpath(
            "//html/body/div/div[4]/div/div/div[4]/div/div[2]/div[3]/div/ul/li[1]/div/div[@class='accordion-content cf']/div[@class='cf']"));
    
    for (WebElement eachElem : listProduct)
    {
        System.out.println("Searching for the exact element:");
    
        if (eachElem.getText().contains(TicketName))
        {
            // Identify the button to click on
            WebElement button = eachElem.findElement(By.xpath(".//span[@class='js-info-pop-up']"));
    
            // WebElement button = eachElem.findElement(By.xpath("//div/div/div[1]/div/div/a/span[contains(text(),'Add to basket')]"));
    
            Thread.sleep(500);
            break; // Break out of iteration.
        }
    }
    
    //列出具有cf类的产品
    List listProduct=driver.findElements(By.xpath(
    “//html/body/div/div[4]/div/div/div[4]/div/div/div[2]/div[3]/div/ul/li[1]/div/div[@class='accordion-content cf']/div[@class='cf']”);
    for(每个Web元素:listProduct)
    {
    System.out.println(“搜索确切的元素:”);
    if(eachElem.getText().contains(TicketName))
    {
    //确定要单击的按钮
    WebElement button=eachElem.findElement(By.xpath(“.//span[@class='js-info-pop-up']);
    //WebElement button=eachElem.findElement(By.xpath(“//div/div/div[1]/div/div/a/span[contains(text(),'Add to basket')]”);
    睡眠(500);
    break;//中断迭代。
    }
    }
    
    我发布了一个我觉得更有用的方法。 我个人喜欢用这种方式使用xpath,因为我发现它更准确,但这可能只是个人的喜好。如果有帮助的话,你可以试一试

    使用pageObjects获取元素的示例:

    public class Grabber {
        /*
         *      There exists a plugin in firefox to right click an element, inspect it, then right clicking the element
         *      and copying the xpath and pasting it here.
         */
    
        private static WebElement element = null;
    
        public static WebElement input_box(WebDriver driver, WebDriverWait wait) {
            wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("XPATH HERE")));
            //Used if element is a button or needs to bet clicked
            //wait.until(ExpectedConditions.elementToBeClickable(By.xpath("XPATH HERE")));
            element = driver.findElement(By.xpath("XPATH HERE"));
            return element;
        }
    
    }
    
    如何使用它:

    编辑:Initialize、NavigateTo和Dispose会给你一个错误,因为它们必须是静态的,我写这篇文章是为了给你一个例子,你应该在你认为合适的时候编辑它,以得到你想要的工作。我希望我为你指出了解决问题的正确方向

    编辑:这里的dispose是在驱动程序完成或抛出异常时除去驱动程序。删除遗漏的临时文件

    public class Test {
    
        private WebDriver driver;
        private WebDriverWait wait;
    
        public static void main(String[] args) {
    
            try {
                initialize();
                navigateTo("www.somewhere.com");
                Grabber.input_box(driver, wait).sendKeys("I want to send these keys");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                dispose();
            }
    
        }
    
        private void initialize() {
            driver = new FirefoxDriver();
            wait = new WebDriverWait(driver, 15);
        }
    
        private void navigateTo(String url) {
            driver.get(url);
        }
    
        private void dispose() {
            RemoteWebDriver cRDriver = (RemoteWebDriver) driver;
            while (cRDriver.getSessionId() != null) {
                driver.quit();
            }
        }
    
    }
    

    我发布了一个我觉得更有用的方法。 我个人喜欢用这种方式使用xpath,因为我发现它更准确,但这可能只是个人的喜好。如果有帮助的话,你可以试一试

    使用pageObjects获取元素的示例:

    public class Grabber {
        /*
         *      There exists a plugin in firefox to right click an element, inspect it, then right clicking the element
         *      and copying the xpath and pasting it here.
         */
    
        private static WebElement element = null;
    
        public static WebElement input_box(WebDriver driver, WebDriverWait wait) {
            wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("XPATH HERE")));
            //Used if element is a button or needs to bet clicked
            //wait.until(ExpectedConditions.elementToBeClickable(By.xpath("XPATH HERE")));
            element = driver.findElement(By.xpath("XPATH HERE"));
            return element;
        }
    
    }
    
    如何使用它:

    编辑:Initialize、NavigateTo和Dispose会给你一个错误,因为它们必须是静态的,我写这篇文章是为了给你一个例子,你应该在你认为合适的时候编辑它,以得到你想要的工作。我希望我为你指出了解决问题的正确方向

    编辑:这里的dispose是在驱动程序完成或抛出异常时除去驱动程序。删除遗漏的临时文件

    public class Test {
    
        private WebDriver driver;
        private WebDriverWait wait;
    
        public static void main(String[] args) {
    
            try {
                initialize();
                navigateTo("www.somewhere.com");
                Grabber.input_box(driver, wait).sendKeys("I want to send these keys");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                dispose();
            }
    
        }
    
        private void initialize() {
            driver = new FirefoxDriver();
            wait = new WebDriverWait(driver, 15);
        }
    
        private void navigateTo(String url) {
            driver.get(url);
        }
    
        private void dispose() {
            RemoteWebDriver cRDriver = (RemoteWebDriver) driver;
            while (cRDriver.getSessionId() != null) {
                driver.quit();
            }
        }
    
    }
    

    您可以尝试以下代码。这将起作用,因为“添加到篮子”不是类。它是范围内的文本

    WebElement button = eachElem.findElement(By.xpath("//span[contains(text(),'Add to basket')]"));   
    button.click(); 
    Thread.Sleep(2000); 
    eachElem.findElement(By.xpath("//span[contains(text(),'Close')]")‌​).Click()
    

    您可以尝试以下代码。这将起作用,因为“添加到篮子”不是类。它是范围内的文本

    WebElement button = eachElem.findElement(By.xpath("//span[contains(text(),'Add to basket')]"));   
    button.click(); 
    Thread.Sleep(2000); 
    eachElem.findElement(By.xpath("//span[contains(text(),'Close')]")‌​).Click()
    
    for(WebElement-eachElem:listProduct)
    {
    字符串tmp=eachElem.findElement(By.xpath(“//h3[contains(,“““+TicketName+”)]”)).getText();
    System.out.println(“产品名称:+tmp”);
    如果(tmp.equals(TicketName)){
    System.out.println(“在IF循环内部”);
    //获取元素的父元素以获取子范围
    WebElement parentEle=eachElem.findElement(By.xpath(“../../…”);
    WebElement button=parentEle.findElement(By.xpath(//span[contains(text(),'Add to basket')]);
    button.click();//如果标题匹配,请单击同一元素的按钮。
    System.out.println(“单击按钮添加篮”);
    break;//中断迭代。
    }
    }
    
    问题似乎主要围绕作为集合获取的元素,即。根据您的xml,此元素没有子元素。要获取元素,您需要获取元素的父元素,即。从该父级,您可以搜索子添加到篮子。如果这个推论有道理,请告诉我。谢谢

    更新:2016年6月24日21:00 我也喜欢一种更通用的方法来查找“父”元素(尽管您可能会有所不同)。对于您的问题,请确认您的“addtobasket”元素位于/a/span(在所有页面中)下,并且类名为“jsinfopop”。任何偏差都可能影响xPath,因为它是相对于currentElement的

      //List products that have class-cf
      List<WebElement> listProduct = driver.findElements(By.xpath("//div/div[@class='accordion-content cf']/div[@class='cf']"));
    
      for(WebElement eachElem:listProduct)
         {   
          System.out.println("Searching for the exact element:");
          if ( eachElem.getText().contains(TicketName))
              {
               //Identify the button to click on
               WebElement button = eachElem.findElement(By.xpath(".//a/span[@class='js-info-pop-up']"));
               Thread.sleep(500);
               button.click();
               break; //Break out of iteration.    
              }        
         }
    
    //列出具有cf类的产品
    List listProduct=driver.findElements(By.xpath(“//div/div[@class='corcordion-content cf']]/div[@class='cf']);
    for(每个Web元素:listProduct)
    {   
    System.out.println(“搜索确切的元素:”);
    if(eachElem.getText().contains(TicketName))
    {
    //将按钮标识为c