繁体   English   中英

使用Selenium Webdriver进行自动化,但无法单击元素

[英]Using selenium webdriver for automation but Not able to click on element

我正在使用Selenium Webdriver在我的网站上执行自动化。 我可以登录网站,但不能对元素执行点击操作。 我的代码尝试是:

WebElement add = BrowserUtilities.driver.findElement(By.xpath("//button[@class = 'btn btn-primary btn-lg']"));
add.click();

我也尝试使用javascript executor如下:

JavascriptExecutor js = (JavascriptExecutor) BrowserUtilities.driver;
js.executeScript("argument[0].click()", add);

现在我在控制台中遇到异常:

FAILED CONFIGURATION: @BeforeClass launchBrowserTest
org.openqa.selenium.WebDriverException: unknown error: argument is not defined

如果有其他解决方案,请提出建议。

这是因为存在javascript和ajax调用,您可以尝试以下代码:

使用webdriver查找元素,等待方式为:

 WebDriverWait wait=new WebDriverWait(driver,50 );           
    WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//button[@type='Cancel']")));

然后使用Actions类执行perfrom click操作:

Actions actions = new Actions(driver);
actions.moveToElement(element).click().build().perfrom();

试试吧。

 WebDriverWait wait = new WebDriverWait(driver,9000);   
 WebElement button=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class = 'btn btn-primary btn-lg']")));
    button.click();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM