简体   繁体   English

Selenium webdriver click() 在 jenkins 中失败

[英]Selenium webdriver click() fails in jenkins

I have written a script for datepicker to choose dates from the calendar.我已经为日期选择器编写了一个脚本来从日历中选择日期。 The scripts are running fine in local, but when I run it through jenkins the script is getting failed.脚本在本地运行良好,但是当我通过 jenkins 运行脚本时,脚本失败了。

action.moveToElement(driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[1]/div/a/span")));//locating the element to click
action.perform();
action.click(driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[1]/div/a/span"))); //this line is not executing
action.perform();

The script to click the element is not working.单击该元素的脚本不起作用。 I am getting error as "Element is not currently visible and so may not be interacted with"我收到error as "Element is not currently visible and so may not be interacted with"

I have also tried driver.我也试过司机。 findElement(By.xpath("//*[@id='ui-datepicker-div']/div[1]/div/a/span")).click() by replacing action.click() but still no use. findElement(By.xpath("//*[@id='ui-datepicker-div']/div[1]/div/a/span")).click() by replacing action.click()但仍然没有使用。

I have faced a similar issue and after a couple of frustrating hours, I have figured out, that im my case only one thing has worked for me - JavascriptExecutor .我遇到了类似的问题,经过几个令人沮丧的小时后,我发现,我的情况只有一件事对我JavascriptExecutor - JavascriptExecutor

I don't know why all other attempts have failed(all of them have worked well locally).我不知道为什么所有其他尝试都失败了(所有这些尝试都在本地运行良好)。 It seems like Jenkins specific issues.这似乎是詹金斯的特定问题。

Anyway you can use this code snippet:无论如何,您可以使用此代码片段:

WebElement elem = driver.findElement(By.xpath("//path/to/element"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", elem);

Note: in my case I was always able to send click action to element, but somehow browser didn't react on this action.注意:在我的情况下,我总是能够将click动作发送到元素,但不知何故浏览器没有对这个动作做出反应。 So element was remained unclicked without any error.所以元素没有被点击,没有任何错误。

You cannot click on the hidden element using selenium because if will throw the exception that you saw.您不能使用 selenium 单击隐藏元素,因为 if 会抛出您看到的异常。 You should either make element visible (in the way the user does so) or use javascript to click (see JavaScript executor).您应该使元素可见(以用户这样做的方式)或使用 javascript 进行单击(请参阅 JavaScript 执行程序)。

Clicking on an element works fine locally but not in Jenkins;单击一个元素在本地可以正常工作,但在 Jenkins 中却不行;

first, I was locating the web element using XPATH, and when it came to clicking I tried selenium click, actions click, js click.首先,我使用 XPATH 定位 web 元素,在点击时,我尝试了 selenium click、actions click、js click。 All were working locally But not in Jenkins.所有人都在本地工作 但不是在 Jenkins。

Finally what worked for me was the combination of css selector with javascriptExecutor click.最后,对我有用的是 css 选择器与 javascriptExecutor click 的组合。 This solved my problem.这解决了我的问题。 Now works both locally and in Jenkins.现在可以在本地和 Jenkins 中使用。

So try the same.所以尝试同样的。

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

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