简体   繁体   English

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

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

I am performing automation on my website using selenium webdriver. 我正在使用Selenium Webdriver在我的网站上执行自动化。 I am able to login into website but not able to perform click operation on element. 我可以登录网站,但不能对元素执行点击操作。 My code attempts are: 我的代码尝试是:

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

I also tried with javascript executor as below : 我也尝试使用javascript executor如下:

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

now I am getting exception in console like: 现在我在控制台中遇到异常:

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

Please suggest me if any other solution. 如果有其他解决方案,请提出建议。

It's because of javascript and ajax call present you can try this code : 这是因为存在javascript和ajax调用,您可以尝试以下代码:

Find Element using webdriver wait as : 使用webdriver查找元素,等待方式为:

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

Then perfrom click operation using Actions class as : 然后使用Actions类执行perfrom click操作:

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

Try with it. 试试吧。

 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