【问题标题】:How to click on the button as per the HTML provided?如何根据提供的 HTML 单击按钮?
【发布时间】:2018-07-30 12:12:43
【问题描述】:

这是弹出窗口中按钮的 html 代码(弹出窗口有一个潜在客户表单)-

<button id="getCoupon" class="fetch" data-bind="click: submitForm" type="submit">Fetch Coupon</button>

这是我在 Eclipse 中用 JAVA 编写的脚本。我可以填写姓名、电子邮件和电话号码,但我无法点击按钮 -

driver.findElement(By.id("getCoupon")).click();

【问题讨论】:

    标签: java selenium selenium-webdriver xpath css-selectors


    【解决方案1】:

    根据评论和网址,您已分享:

    你可以试试这段代码:

    public class Pawan  {
    
        static WebDriver driver ; 
    
        public static void main(String[] args) throws InterruptedException {
            System.setProperty("webdriver.chrome.driver", "C:\\Users\\user**\\Downloads\\chromedriver_win32\\chromedriver.exe");
            driver = new ChromeDriver();
            driver.manage().window().maximize();
            driver.get("http://vets.cm.qa.preview.vca.webstagesite.com/free-first-exam");
            WebDriverWait wait = new WebDriverWait(driver, 10);
    
            wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[value='/santa-monica']~div.select-btn"))).click();
            wait.until(ExpectedConditions.elementToBeClickable(By.id("fName"))).sendKeys("Pawan");
            wait.until(ExpectedConditions.elementToBeClickable(By.id("lName"))).sendKeys("Sharma");
            wait.until(ExpectedConditions.elementToBeClickable(By.id("email"))).sendKeys("ps12@gmail.com");
            wait.until(ExpectedConditions.elementToBeClickable(By.id("zip"))).sendKeys("90404");
            wait.until(ExpectedConditions.elementToBeClickable(By.id("phone"))).sendKeys("9697989900");  
    
            JavascriptExecutor jse = (JavascriptExecutor)driver;
            jse.executeScript("window.scrollBy(0,100)", "");
    
            wait.until(ExpectedConditions.elementToBeClickable(By.id("getCoupon"))).click();    
        }
    }
    

    【讨论】:

    • 嗨,它工作得很好,但我在 Firefox 中检查过!它对你有用吗?
    • @Rajagopalan :我已经在 chrome 中检查了这个,但它对我来说很好用。感谢您的帮助。我只是想验证一下,现在由 OP 决定。
    • 好吧,我猜他犯了一些小错误!
    • @Rajagopalan:可能是。这就是为什么我给了他完整的代码。让我们看看
    • 我已经给出了,这是一个更大的对话,它被转移到聊天部分,它不适合他!我检查了浏览器 chrome 和 firefox,但似乎没有什么对他有用。
    【解决方案2】:

    根据您的 HTML 代码,您的 id 是:“getCoupon”, 而在代码中,您提到 id 为:“getCouponFetch”。请更正,它应该工作。代码 -

    driver.findElement(By.id("getCoupon")).click();
    

    如果 selenium 点击不起作用,请使用下面的 Java Script 点击代码:

    WebElement element = driver.findElement(By.id("getCoupon")); JavascriptExecutor executor = (JavascriptExecutor)driver; executor.executeScript("arguments[0].click();", element);
    

    【讨论】:

    • 评论不用于扩展讨论;这个对话是moved to chat
    • 我看到你已经写信给@DebanjanB,但是你没有在他的名字前面加上一个@符号,他可能会来帮你!
    • 它非常适合我,所以我无法帮助你,对不起!我希望@DebanjanB 能帮到你!
    • @Rajagopalan :你能帮我一个忙吗,只需运行我的代码来回答这个问题,如果执行成功,请告诉我。谢谢!
    • @cruisepandey 好的,当然!
    【解决方案3】:

    首先,你得到了什么样的错误??

    如果您得到如下所示的“NoSuchElementException”,请尝试使用隐式等待:

    driver.manage().timeOuts().implicitlywait(30,TimeUnit.SECONDS);
    

    然后尝试使用以下方式找到该按钮:

    driver.findElementByXpath("text()[contains(.,'Fetch Coupon')]").click();
    

    【讨论】:

      【解决方案4】:

      根据您在按钮上共享给click()HTML,您必须诱导 WebDriverWait 以使所需的元素可点击您可以使用以下任一解决方案:

      • cssSelector:

        new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.fetch#getCoupon"))).click();
        
      • xpath:

        new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='fetch' and @id='getCoupon'][contains(.,'Fetch Coupon')]"))).click();
        

      更新

      您也可以使用executeScript() 方法调用click(),如下所示:

      • 使用cssSelector

        WebElement fetch_coupon = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.fetch#getCoupon")));
        ((JavascriptExecutor)driver).executeScript("arguments[0].click();", fetch_coupon);      
        
      • 使用xpath

        WebElement fetch_coupon = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='fetch' and @id='getCoupon'][contains(.,'Fetch Coupon')]")));
        ((JavascriptExecutor)driver).executeScript("arguments[0].click();", fetch_coupon);
        

      【讨论】:

      • @PawanSharma :当您说这不起作用时,这毫无意义。表现出一些努力。如果有,您应该发布错误堆栈跟踪。
      • @debanjan 我没有收到任何错误。问题是脚本没有点击按钮。
      • 您能否准确地更新问题,您要在 网页 上单击哪个元素?
      • 我正在尝试点击这个按钮:
      • 此元素在网页上不可见,请在尝试单击该元素之前使用手动步骤更新问题。
      猜你喜欢
      • 1970-01-01
      • 2019-01-06
      • 1970-01-01
      • 2019-01-08
      • 2019-02-15
      • 1970-01-01
      • 2010-09-23
      • 2019-01-31
      • 1970-01-01
      相关资源
      最近更新 更多