英文:
Selenium cannot find element from time to time
问题
以下是您提供的内容的翻译部分:
我使用Selenium与Jira进行交互。我的应用程序从Google Sheets读取时间日志,并将它们插入Jira时间记录器(非标准Jira插件)。
问题在于,Selenium在我尝试点击TimeLog插件时,有时会返回以下异常(其他阶段正常工作):
Exception in thread "main" org.openqa.selenium.TimeoutException: 预期条件失败:等待通过By.xpath定位的元素可见://div[@class='sc-bvODop kOWqZw' and @xpath='1'](尝试40秒(间隔500毫秒))
at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:95)
...
...
Caused by: org.openqa.selenium.NoSuchElementException: 没有这样的元素:无法定位元素:{"method":"xpath","selector":"//div[@class='sc-bvODop kOWqZw' and @xpath='1']"}
...
我可以运行应用程序的前10-15次,它可以正常工作,然后我会看到这样的异常。当我手动关闭打开的驱动程序(通过任务管理器)并清除Windows和Chrome临时文件时,它可能会再次给我几次运行应用程序的机会。然后我会收到相同的异常。
我尝试使用不同的drivers
(MS Edge),检查相对的xPath
,使用不同的waiting
策略。
public void runSelenium(WebDriver driver) throws InterruptedException, IOException, GeneralSecurityException {
...
// 阶段上面的内容都正常工作
if (isNewLogsToInput()) {
inputNewLogs(driver);
}
}
private void inputNewLogs(WebDriver driver) throws InterruptedException, IOException, GeneralSecurityException {
...
for (int i = startPosition; i < entriesNumber; i++) {
openTimeLog(driver); // 这里出现问题
...
}
}
private void openTimeLog(WebDriver driver) {
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement button = wait.until(ExpectedConditions.visibilityOfElementLocated(
By.xpath("//body/div[13]/div[3]/div[1]/div[3]/div[2]/div[1]/div[1]/div[2]/div[1]/div[1]/div[2]/div[1]/div[1]/div[1]/div[1]/div[2]/div[1]/div[1]/div[2]/div[1]/div[1]/div[2]/div[1]/div[2]/div[1]/div[1]/div[1]/div[1]/div[1]/div[2]/small[1]")));
button.click();
}
<div class="sc-bIZIuE bmcBym" data-test-id="issue.views.issue-base.context.context-items.primary-items">
...
<div class="sc-bvODop kOWqZw">
...
<button class="EditButton-sc-1v6bv8a-0 doKTJP" aria-label="Edit Time tracking" type="button"></button>
</div>
...
</div>
我无法理解我做错了什么...
1: https://i.stack.imgur.com/jh1Up.png
英文:
I use Selenium to interact with Jira. My application reads time logs from Google Sheets and inserts them into Jira time logger (non-standard Jira plugin).
The problem is that Selenium returns following exeption from time to time on the stage when I try to click on the TimeLog plugin (other stages work properly)
Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.xpath: //div[@class='sc-bvODop kOWqZw' and @xpath='1'] (tried for 40 second(s) with 500 milliseconds interval)
at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:95)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:272)
at com.fox.jiratimelog.selenium.SeleniumOperator.openTimeLog(SeleniumOperator.java:143)
at com.fox.jiratimelog.selenium.SeleniumOperator.inputNewLogs(SeleniumOperator.java:60)
at com.fox.jiratimelog.selenium.SeleniumOperator.runSelenium(SeleniumOperator.java:50)
at com.fox.jiratimelog.JiraTimeLogApplication.main(JiraTimeLogApplication.java:24)
Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@class='sc-bvODop kOWqZw' and @xpath='1']"}
(Session info: chrome=86.0.4240.75)
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'DESKTOP-OFG72V5', ip: '192.168.0.105', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '14.0.1'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 86.0.4240.75, chrome: {chromedriverVersion: 85.0.4183.87 (cd6713ebf92fa..., userDataDir: C:\Users\Max\AppData\Local\...}, goog:chromeOptions: {debuggerAddress: localhost:49319}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:virtualAuthenticators: true}
Session ID: e3a81681e9e6873351bab1211c3386e7
*** Element info: {Using=xpath, value=//div[@class='sc-bvODop kOWqZw' and @xpath='1']}
at jdk.internal.reflect.GeneratedConstructorAccessor21.newInstance(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:323)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:428)
at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315)
at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:205)
at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:201)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:249)
... 4 more
I can run application for the first 10-15 times and it works properly, then I see such an exception.
When I close open drivers manually (via Task manager) and clean Windows and Chrome temporary files it may give me several more times to run application. Then I receive the same exception.
I tried to use different drivers
(MS Edge), checked relative xPath
, use different waiting
strategies
public void runSelenium(WebDriver driver) throws InterruptedException, IOException, GeneralSecurityException {
if (!driver.getTitle().equals("Google")) {
driver.manage().window().maximize();
}
enterLogin(driver);
Thread.sleep(2000);
enterPassword(driver);
Thread.sleep(7000);
openProject(driver);
Thread.sleep(3000);
openIssue(driver);
Thread.sleep(5000);
// stages above work properly
if (isNewLogsToInput()) {
inputNewLogs(driver);
}
}
private void inputNewLogs(WebDriver driver) throws InterruptedException, IOException, GeneralSecurityException {
int entriesNumber = reader.getEntriesNumberFromSheets();
int numberOfNewLogs = entriesNumber - logs.getTimeLogsQuantity();
int startPosition = entriesNumber - numberOfNewLogs;
for (int i = startPosition; i < entriesNumber; i++) {
openTimeLog(driver); // HERE I HAVE A PROBLEM
Thread.sleep(3000);
inputTimeSpent(driver, i);
Thread.sleep(2000);
inputTimeRemaining(driver, i);
Thread.sleep(2000);
inputDateStarted(driver, i);
Thread.sleep(2000);
inputTimeStarted(driver, i);
Thread.sleep(2000);
inputWorkDescription(driver, i);
Thread.sleep(2000);
saveTime(driver);
}
}
private void openTimeLog(WebDriver driver) {
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement button = wait.until(ExpectedConditions.visibilityOfElementLocated(
By.xpath("//body/div[13]/div[3]/div[1]/div[3]/div[2]/div[1]/div[1]/div[2]/div[1]/div[1]/div[2]/div[1]/div[1]/div[1]/div[1]/div[2]/div[1]/div[1]/div[2]/div[1]/div[1]/div[2]/div[1]/div[2]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[2]/small[1]")));
button.click();
}
<div class="sc-bIZIuE bmcBym" data-test-id="issue.views.issue-base.context.context-items.primary-items">
<div>
<div class="sc-LAuEU kNfSUt"><h2 class="sc-hrBRpH juzrVt">Time tracking</h2></div>
<div class="sc-bvODop kOWqZw">
<div class="RootWrapper-sc-1va80k6-0 fsVJT">
<div style="position: absolute;"><label class="Label__LabelWrapper-sc-17towfw-0 keOXhT">
<div class="Label__LabelInner-sc-17towfw-1 jbyPaz"><span>Time tracking</span></div>
</label></div>
<div class="ContentWrapper-kdagst-0 fPLsyX">
<div class="FieldBaseWrapper-sc-14kmybr-0 cspqsP">
<div class="Content__ContentWrapper-ve26fj-2 feWTNh">
<div class="Content__ChildWrapper-ve26fj-0 ilJUcG">
<div class="Content-ve26fj-1 llCsQv">
<div class="ReadViewContentWrapper-xymwx5-0 kLiHRY">
<div class="sc-eAyhxF hdkDWI">
<div class="sc-kbdWBx dpBQSe"><span class="sc-kGXeez gTVGyF"
role="presentation"><object
data="https://jira-frontend-static.prod.public.atl-paas.net/assets/stopwatch-glyph.abbc4afde0b6bdac7465517877d67af5.8.svg"
width="24" height="24" type="image/svg+xml"></object></span></div>
<div class="sc-eVrGFk bqZIdq">
<div class="sc-fdqjUm jBnpdo">
<div class="sc-cLmFfZ cjohbD" color="#0052CC"
width="22.669735327963174%"></div>
<div class="sc-cLmFfZ csMpYM" color="#FFAB00" width="0"></div>
</div>
<small class="sc-iNovjJ gmkLRu" xpath="1"><span
class="sc-kyCyAI gZEfLW"><span><span class="sc-dchYKM ENMBd">2d 25m</span> logged</span></span><span
class="sc-fEVUGC eXwoSi"><span><span class="sc-dchYKM ENMBd">1w 2d</span> remaining</span></span></small>
</div>
</div>
<button class="EditButton-sc-1v6bv8a-0 doKTJP" aria-label="Edit Time tracking"
type="button"></button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div class="sc-LAuEU kNfSUt"><h2 class="sc-hrBRpH juzrVt">Assignee</h2></div>
<div class="sc-eZXMBi iWQVSp" data-test-id="issue.views.field.user.assignee">
<div class="RootWrapper-sc-1va80k6-0 fsVJT">
<div style="position: absolute;"><label class="Label__LabelWrapper-sc-17towfw-0 keOXhT">
<div class="Label__LabelInner-sc-17towfw-1 jbyPaz"><span>Assignee</span></div>
</label></div>
<div class="ContentWrapper-kdagst-0 fPLsyX">
<div class="FieldBaseWrapper-sc-14kmybr-0 cspqsP">
<div class="Content__ContentWrapper-ve26fj-2 feWTNh">
<div class="Content__ChildWrapper-ve26fj-0 ilJUcG">
<div class="Content-ve26fj-1 llCsQv">
<div class="ReadViewContentWrapper-xymwx5-0 kLiHRY">
<div class="sc-iBlNuT fGlWRy">
<div class="sc-iXKTDd ferkkY">
<div data-test-id="profilecard-next.ui.profilecard.profilecard-trigger">
<div class="sc-kiXyGy iYsZzI">
<div class="sc-ekQYnd gFYdNG">
<div style="display: inline-block; position: relative; outline: 0px;">
<span class="css-1c24z73"><span class="css-zk1cl3"
role="img"
aria-label=""></span></span>
</div>
</div>
<div class="SingleLineTextInput__ReadView-sc-4hfvq0-0 epXzqq">
Test test
</div>
</div>
</div>
</div>
</div>
<button class="EditButton-sc-1v6bv8a-0 doKTJP" aria-label="Edit Assignee"
type="button"></button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div class="sc-LAuEU kNfSUt"><h2 class="sc-hrBRpH juzrVt">Labels</h2></div>
<div>
<div class="sc-bvODop kOWqZw">
<div class="RootWrapper-sc-1va80k6-0 fsVJT">
<div style="position: absolute;"><label class="Label__LabelWrapper-sc-17towfw-0 keOXhT">
<div class="Label__LabelInner-sc-17towfw-1 jbyPaz"><span>Labels</span></div>
</label></div>
<div class="ContentWrapper-kdagst-0 fPLsyX">
<div class="FieldBaseWrapper-sc-14kmybr-0 cspqsP">
<div class="Content__ContentWrapper-ve26fj-2 feWTNh">
<div class="Content__ChildWrapper-ve26fj-0 ilJUcG">
<div class="Content-ve26fj-1 llCsQv">
<div class="ReadViewContentWrapper-xymwx5-0 kLiHRY"><span
class="sc-esExBO gIsNhp">None</span>
<button class="EditButton-sc-1v6bv8a-0 doKTJP" aria-label="Edit Labels"
type="button"></button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div class="sc-LAuEU kNfSUt"><h2 class="sc-hrBRpH juzrVt">Sprint</h2></div>
<div>
<div class="sc-bvODop kOWqZw">
<div class="RootWrapper-sc-1va80k6-0 fsVJT">
<div style="position: absolute;"><label class="Label__LabelWrapper-sc-17towfw-0 keOXhT">
<div class="Label__LabelInner-sc-17towfw-1 jbyPaz"><span>Sprint</span></div>
</label></div>
<div class="ContentWrapper-kdagst-0 fPLsyX">
<div class="FieldBaseWrapper-sc-14kmybr-0 cspqsP">
<div class="Content__ContentWrapper-ve26fj-2 feWTNh">
<div class="Content__ChildWrapper-ve26fj-0 ilJUcG">
<div class="Content-ve26fj-1 llCsQv">
<div class="ReadViewContentWrapper-xymwx5-0 kLiHRY">
<div class="sc-hQDGvh fijXMI">
<div class="sc-eYdvao dhbZhl"><a>FOX Sprint 1</a></div>
</div>
<button class="EditButton-sc-1v6bv8a-0 doKTJP" aria-label="Edit Sprint"
type="button"></button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sc-eePzDA jCyCpj">
<div class="sc-LAuEU kNfSUt"><h2 class="sc-hrBRpH juzrVt">Story point estimate</h2></div>
<div>
<div class="RootWrapper-sc-1va80k6-0 fsVJT">
<div style="position: absolute;"><label class="Label__LabelWrapper-sc-17towfw-0 keOXhT">
<div class="Label__LabelInner-sc-17towfw-1 jbyPaz"><span>Story point estimate</span></div>
</label></div>
<div class="ContentWrapper-kdagst-0 fPLsyX">
<div class="FieldBaseWrapper-sc-14kmybr-0 cspqsP">
<div class="Content__ContentWrapper-ve26fj-2 feWTNh">
<div class="Content__ChildWrapper-ve26fj-0 ilJUcG">
<div class="Content-ve26fj-1 llCsQv">
<div class="ReadViewContentWrapper-xymwx5-0 kLiHRY"><span
class="sc-hmzhuo igqoCl">8</span>
<button class="EditButton-sc-1v6bv8a-0 doKTJP" aria-label="Edit"
type="button"></button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div class="sc-LAuEU kNfSUt"><h2 class="sc-hrBRpH juzrVt">Reporter</h2></div>
<div class="sc-eZXMBi iWQVSp" data-test-id="issue.views.field.user.reporter">
<div class="RootWrapper-sc-1va80k6-0 fsVJT">
<div style="position: absolute;"><label class="Label__LabelWrapper-sc-17towfw-0 keOXhT">
<div class="Label__LabelInner-sc-17towfw-1 jbyPaz"><span>Reporter</span></div>
</label></div>
<div class="ContentWrapper-kdagst-0 fPLsyX">
<div class="FieldBaseWrapper-sc-14kmybr-0 cspqsP">
<div class="Content__ContentWrapper-ve26fj-2 feWTNh">
<div class="Content__ChildWrapper-ve26fj-0 ilJUcG">
<div class="Content-ve26fj-1 llCsQv">
<div class="ReadViewContentWrapper-xymwx5-0 kLiHRY">
<div class="sc-iBlNuT fGlWRy">
<div class="sc-iXKTDd ferkkY">
<div data-test-id="profilecard-next.ui.profilecard.profilecard-trigger">
<div class="sc-kiXyGy iYsZzI">
<div class="sc-ekQYnd gFYdNG">
<div style="display: inline-block; position: relative; outline: 0px;">
<span class="css-1c24z73"><span class="css-zk1cl3"
role="img"
aria-label=""></span></span>
</div>
</div>
<div class="SingleLineTextInput__ReadView-sc-4hfvq0-0 epXzqq">
Test test
</div>
</div>
</div>
</div>
</div>
<button class="EditButton-sc-1v6bv8a-0 doKTJP" aria-label="Edit Reporter"
type="button"></button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div id="releases-panel-container" class="sc-jHsedM dDOmWr" data-test-id="development.releases-glance.actions">
<div class="sc-jeZOwl eFGvoQ"><h2 class="sc-buBCPq fOTSoR">Releases</h2></div>
<div class="sc-cPwpjd egqpOy"></div>
</div>
</div>
</div>
I cannot understand what I'm doing wrong...
答案1
得分: 1
你至少有三个错误:
- 使用固定的休眠时间。
这不是一个好的做法,因为在很大一部分时间内,你的测试什么都没在做。改为使用更灵活的休眠方法。
- 使用脆弱的XPath。
//body/div[13]/div[3]/div[1]/div[3]/div[2]/div[1]/div[1]/div[2]/div[1]/div[1]/div[2]/div[1]/div[1]/div[1]/div[1]/div[2]/div[1]/div[1]/div[2]/div[1]/div[1]/div[2]/div[1]/div[2]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[2]/small[1]
很容易就会失效。如果你能提供页面的HTML,我们可以尝试找到更好的定位方法。
- 可能是等待时间过短。
WebDriverWait wait = new WebDriverWait(driver, 20);
是比使用 Thread.sleep()
更好的方法。然而,20秒可能是一个很短的时间,这取决于网络速度等因素。尝试使用更长的超时时间。
编辑:
通过使用CSS选择器而不是XPath选择器解决了这个问题:
By.cssSelector("div[class*='Content__ChildWrapper'] small")
英文:
There are at least three things you are doing wrong:
- Using fixed sleeps.
This is not a good practice because for a good chunk of time your test is not doing anything. Use more flexible sleeps instead.
- Using fragile xpath.
//body/div[13]/div[3]/div[1]/div[3]/div[2]/div[1]/div[1]/div[2]/div[1]/div[1]/div[2]/div[1]/div[1]/div[1]/div[1]/div[2]/div[1]/div[1]/div[2]/div[1]/div[1]/div[2]/div[1]/div[2]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[2]/small[1]
could break easily. If you can provide the HTML of the page, we could try to find a better locator.
- Potentially a short wait.
This WebDriverWait wait = new WebDriverWait(driver, 20);
is a much better approach than using Thread.sleep()
. However, 20 seconds could be a short time, depending on the network speed, for example. Try using a longer timeout.
EDIT:
The problem was solved by using a CSS selector instead of XPATH selector:
By.cssSelector("div[class*='Content__ChildWrapper'] small")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论