这是弹出窗口中按钮的html代码(弹出窗口具有潜在客户形式)-
<button id="getCoupon" class="fetch" data-bind="click: submitForm" type="submit">Fetch Coupon</button>
这是我在Eclipse中用JAVA编写的脚本。我可以填写姓名,电子邮件和电话号码,但不能单击按钮-
driver.findElement(By.id("getCoupon")).click();
答案 0 :(得分:0)
根据您的HTML代码,您的ID为:“ getCoupon”, 而在代码中,您提到的id为:“ getCouponFetch”。请更正,它应该可以工作。代码-
driver.findElement(By.id("getCoupon")).click();
如果硒单击不起作用,请使用下面的Java脚本单击代码:
WebElement element = driver.findElement(By.id("getCoupon")); JavascriptExecutor executor = (JavascriptExecutor)driver; executor.executeScript("arguments[0].click();", element);
答案 1 :(得分:0)
根据您与按钮上的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);
答案 2 :(得分:0)
首先,您遇到了什么样的错误?
如果收到如下所示的“ NoSuchElementException ”,请尝试使用隐式等待:
driver.manage().timeOuts().implicitlywait(30,TimeUnit.SECONDS);
然后尝试使用以下方式找到该按钮:
driver.findElementByXpath("text()[contains(.,'Fetch Coupon')]").click();
答案 3 :(得分:0)
根据评论和URL,您已共享:
您可以尝试使用以下代码:
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();
}
}