Selenium有时找不到元素。

huangapple go评论78阅读模式
英文:

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 &quot;main&quot; org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.xpath: //div[@class=&#39;sc-bvODop kOWqZw&#39; and @xpath=&#39;1&#39;] (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: {&quot;method&quot;:&quot;xpath&quot;,&quot;selector&quot;:&quot;//div[@class=&#39;sc-bvODop kOWqZw&#39; and @xpath=&#39;1&#39;]&quot;}
  (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: &#39;3.141.59&#39;, revision: &#39;e82be7d358&#39;, time: &#39;2018-11-14T08:17:03&#39;
System info: host: &#39;DESKTOP-OFG72V5&#39;, ip: &#39;192.168.0.105&#39;, os.name: &#39;Windows 10&#39;, os.arch: &#39;amd64&#39;, os.version: &#39;10.0&#39;, java.version: &#39;14.0.1&#39;
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=&#39;sc-bvODop kOWqZw&#39; and @xpath=&#39;1&#39;]}
	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(&quot;Google&quot;)) {
            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 &lt; 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(&quot;//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]&quot;)));
        button.click();
    }

Screen with problem

&lt;div class=&quot;sc-bIZIuE bmcBym&quot; data-test-id=&quot;issue.views.issue-base.context.context-items.primary-items&quot;&gt;
    &lt;div&gt;
        &lt;div class=&quot;sc-LAuEU kNfSUt&quot;&gt;&lt;h2 class=&quot;sc-hrBRpH juzrVt&quot;&gt;Time tracking&lt;/h2&gt;&lt;/div&gt;
        &lt;div class=&quot;sc-bvODop kOWqZw&quot;&gt;
            &lt;div class=&quot;RootWrapper-sc-1va80k6-0 fsVJT&quot;&gt;
                &lt;div style=&quot;position: absolute;&quot;&gt;&lt;label class=&quot;Label__LabelWrapper-sc-17towfw-0 keOXhT&quot;&gt;
                    &lt;div class=&quot;Label__LabelInner-sc-17towfw-1 jbyPaz&quot;&gt;&lt;span&gt;Time tracking&lt;/span&gt;&lt;/div&gt;
                &lt;/label&gt;&lt;/div&gt;
                &lt;div class=&quot;ContentWrapper-kdagst-0 fPLsyX&quot;&gt;
                    &lt;div class=&quot;FieldBaseWrapper-sc-14kmybr-0 cspqsP&quot;&gt;
                        &lt;div class=&quot;Content__ContentWrapper-ve26fj-2 feWTNh&quot;&gt;
                            &lt;div class=&quot;Content__ChildWrapper-ve26fj-0 ilJUcG&quot;&gt;
                                &lt;div class=&quot;Content-ve26fj-1 llCsQv&quot;&gt;
                                    &lt;div class=&quot;ReadViewContentWrapper-xymwx5-0 kLiHRY&quot;&gt;
                                        &lt;div class=&quot;sc-eAyhxF hdkDWI&quot;&gt;
                                            &lt;div class=&quot;sc-kbdWBx dpBQSe&quot;&gt;&lt;span class=&quot;sc-kGXeez gTVGyF&quot;
                                                                                role=&quot;presentation&quot;&gt;&lt;object
                                                    data=&quot;https://jira-frontend-static.prod.public.atl-paas.net/assets/stopwatch-glyph.abbc4afde0b6bdac7465517877d67af5.8.svg&quot;
                                                    width=&quot;24&quot; height=&quot;24&quot; type=&quot;image/svg+xml&quot;&gt;&lt;/object&gt;&lt;/span&gt;&lt;/div&gt;
                                            &lt;div class=&quot;sc-eVrGFk bqZIdq&quot;&gt;
                                                &lt;div class=&quot;sc-fdqjUm jBnpdo&quot;&gt;
                                                    &lt;div class=&quot;sc-cLmFfZ cjohbD&quot; color=&quot;#0052CC&quot;
                                                         width=&quot;22.669735327963174%&quot;&gt;&lt;/div&gt;
                                                    &lt;div class=&quot;sc-cLmFfZ csMpYM&quot; color=&quot;#FFAB00&quot; width=&quot;0&quot;&gt;&lt;/div&gt;
                                                &lt;/div&gt;
                                                &lt;small class=&quot;sc-iNovjJ gmkLRu&quot; xpath=&quot;1&quot;&gt;&lt;span
                                                        class=&quot;sc-kyCyAI gZEfLW&quot;&gt;&lt;span&gt;&lt;span class=&quot;sc-dchYKM ENMBd&quot;&gt;2d 25m&lt;/span&gt; logged&lt;/span&gt;&lt;/span&gt;&lt;span
                                                        class=&quot;sc-fEVUGC eXwoSi&quot;&gt;&lt;span&gt;&lt;span class=&quot;sc-dchYKM ENMBd&quot;&gt;1w 2d&lt;/span&gt; remaining&lt;/span&gt;&lt;/span&gt;&lt;/small&gt;
                                            &lt;/div&gt;
                                        &lt;/div&gt;
                                        &lt;button class=&quot;EditButton-sc-1v6bv8a-0 doKTJP&quot; aria-label=&quot;Edit Time tracking&quot;
                                                type=&quot;button&quot;&gt;&lt;/button&gt;
                                    &lt;/div&gt;
                                &lt;/div&gt;
                            &lt;/div&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div&gt;
        &lt;div&gt;
            &lt;div class=&quot;sc-LAuEU kNfSUt&quot;&gt;&lt;h2 class=&quot;sc-hrBRpH juzrVt&quot;&gt;Assignee&lt;/h2&gt;&lt;/div&gt;
            &lt;div class=&quot;sc-eZXMBi iWQVSp&quot; data-test-id=&quot;issue.views.field.user.assignee&quot;&gt;
                &lt;div class=&quot;RootWrapper-sc-1va80k6-0 fsVJT&quot;&gt;
                    &lt;div style=&quot;position: absolute;&quot;&gt;&lt;label class=&quot;Label__LabelWrapper-sc-17towfw-0 keOXhT&quot;&gt;
                        &lt;div class=&quot;Label__LabelInner-sc-17towfw-1 jbyPaz&quot;&gt;&lt;span&gt;Assignee&lt;/span&gt;&lt;/div&gt;
                    &lt;/label&gt;&lt;/div&gt;
                    &lt;div class=&quot;ContentWrapper-kdagst-0 fPLsyX&quot;&gt;
                        &lt;div class=&quot;FieldBaseWrapper-sc-14kmybr-0 cspqsP&quot;&gt;
                            &lt;div class=&quot;Content__ContentWrapper-ve26fj-2 feWTNh&quot;&gt;
                                &lt;div class=&quot;Content__ChildWrapper-ve26fj-0 ilJUcG&quot;&gt;
                                    &lt;div class=&quot;Content-ve26fj-1 llCsQv&quot;&gt;
                                        &lt;div class=&quot;ReadViewContentWrapper-xymwx5-0 kLiHRY&quot;&gt;
                                            &lt;div class=&quot;sc-iBlNuT fGlWRy&quot;&gt;
                                                &lt;div class=&quot;sc-iXKTDd ferkkY&quot;&gt;
                                                    &lt;div data-test-id=&quot;profilecard-next.ui.profilecard.profilecard-trigger&quot;&gt;
                                                        &lt;div class=&quot;sc-kiXyGy iYsZzI&quot;&gt;
                                                            &lt;div class=&quot;sc-ekQYnd gFYdNG&quot;&gt;
                                                                &lt;div style=&quot;display: inline-block; position: relative; outline: 0px;&quot;&gt;
                                                                    &lt;span class=&quot;css-1c24z73&quot;&gt;&lt;span class=&quot;css-zk1cl3&quot;
                                                                                                    role=&quot;img&quot;
                                                                                                    aria-label=&quot;&quot;&gt;&lt;/span&gt;&lt;/span&gt;
                                                                &lt;/div&gt;
                                                            &lt;/div&gt;
                                                            &lt;div class=&quot;SingleLineTextInput__ReadView-sc-4hfvq0-0 epXzqq&quot;&gt;
                                                                Test test
                                                            &lt;/div&gt;
                                                        &lt;/div&gt;
                                                    &lt;/div&gt;
                                                &lt;/div&gt;
                                            &lt;/div&gt;
                                            &lt;button class=&quot;EditButton-sc-1v6bv8a-0 doKTJP&quot; aria-label=&quot;Edit Assignee&quot;
                                                    type=&quot;button&quot;&gt;&lt;/button&gt;
                                        &lt;/div&gt;
                                    &lt;/div&gt;
                                &lt;/div&gt;
                            &lt;/div&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div&gt;
        &lt;div class=&quot;sc-LAuEU kNfSUt&quot;&gt;&lt;h2 class=&quot;sc-hrBRpH juzrVt&quot;&gt;Labels&lt;/h2&gt;&lt;/div&gt;
        &lt;div&gt;
            &lt;div class=&quot;sc-bvODop kOWqZw&quot;&gt;
                &lt;div class=&quot;RootWrapper-sc-1va80k6-0 fsVJT&quot;&gt;
                    &lt;div style=&quot;position: absolute;&quot;&gt;&lt;label class=&quot;Label__LabelWrapper-sc-17towfw-0 keOXhT&quot;&gt;
                        &lt;div class=&quot;Label__LabelInner-sc-17towfw-1 jbyPaz&quot;&gt;&lt;span&gt;Labels&lt;/span&gt;&lt;/div&gt;
                    &lt;/label&gt;&lt;/div&gt;
                    &lt;div class=&quot;ContentWrapper-kdagst-0 fPLsyX&quot;&gt;
                        &lt;div class=&quot;FieldBaseWrapper-sc-14kmybr-0 cspqsP&quot;&gt;
                            &lt;div class=&quot;Content__ContentWrapper-ve26fj-2 feWTNh&quot;&gt;
                                &lt;div class=&quot;Content__ChildWrapper-ve26fj-0 ilJUcG&quot;&gt;
                                    &lt;div class=&quot;Content-ve26fj-1 llCsQv&quot;&gt;
                                        &lt;div class=&quot;ReadViewContentWrapper-xymwx5-0 kLiHRY&quot;&gt;&lt;span
                                                class=&quot;sc-esExBO gIsNhp&quot;&gt;None&lt;/span&gt;
                                            &lt;button class=&quot;EditButton-sc-1v6bv8a-0 doKTJP&quot; aria-label=&quot;Edit Labels&quot;
                                                    type=&quot;button&quot;&gt;&lt;/button&gt;
                                        &lt;/div&gt;
                                    &lt;/div&gt;
                                &lt;/div&gt;
                            &lt;/div&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div&gt;
        &lt;div class=&quot;sc-LAuEU kNfSUt&quot;&gt;&lt;h2 class=&quot;sc-hrBRpH juzrVt&quot;&gt;Sprint&lt;/h2&gt;&lt;/div&gt;
        &lt;div&gt;
            &lt;div class=&quot;sc-bvODop kOWqZw&quot;&gt;
                &lt;div class=&quot;RootWrapper-sc-1va80k6-0 fsVJT&quot;&gt;
                    &lt;div style=&quot;position: absolute;&quot;&gt;&lt;label class=&quot;Label__LabelWrapper-sc-17towfw-0 keOXhT&quot;&gt;
                        &lt;div class=&quot;Label__LabelInner-sc-17towfw-1 jbyPaz&quot;&gt;&lt;span&gt;Sprint&lt;/span&gt;&lt;/div&gt;
                    &lt;/label&gt;&lt;/div&gt;
                    &lt;div class=&quot;ContentWrapper-kdagst-0 fPLsyX&quot;&gt;
                        &lt;div class=&quot;FieldBaseWrapper-sc-14kmybr-0 cspqsP&quot;&gt;
                            &lt;div class=&quot;Content__ContentWrapper-ve26fj-2 feWTNh&quot;&gt;
                                &lt;div class=&quot;Content__ChildWrapper-ve26fj-0 ilJUcG&quot;&gt;
                                    &lt;div class=&quot;Content-ve26fj-1 llCsQv&quot;&gt;
                                        &lt;div class=&quot;ReadViewContentWrapper-xymwx5-0 kLiHRY&quot;&gt;
                                            &lt;div class=&quot;sc-hQDGvh fijXMI&quot;&gt;
                                                &lt;div class=&quot;sc-eYdvao dhbZhl&quot;&gt;&lt;a&gt;FOX Sprint 1&lt;/a&gt;&lt;/div&gt;
                                            &lt;/div&gt;
                                            &lt;button class=&quot;EditButton-sc-1v6bv8a-0 doKTJP&quot; aria-label=&quot;Edit Sprint&quot;
                                                    type=&quot;button&quot;&gt;&lt;/button&gt;
                                        &lt;/div&gt;
                                    &lt;/div&gt;
                                &lt;/div&gt;
                            &lt;/div&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;sc-eePzDA jCyCpj&quot;&gt;
        &lt;div class=&quot;sc-LAuEU kNfSUt&quot;&gt;&lt;h2 class=&quot;sc-hrBRpH juzrVt&quot;&gt;Story point estimate&lt;/h2&gt;&lt;/div&gt;
        &lt;div&gt;
            &lt;div class=&quot;RootWrapper-sc-1va80k6-0 fsVJT&quot;&gt;
                &lt;div style=&quot;position: absolute;&quot;&gt;&lt;label class=&quot;Label__LabelWrapper-sc-17towfw-0 keOXhT&quot;&gt;
                    &lt;div class=&quot;Label__LabelInner-sc-17towfw-1 jbyPaz&quot;&gt;&lt;span&gt;Story point estimate&lt;/span&gt;&lt;/div&gt;
                &lt;/label&gt;&lt;/div&gt;
                &lt;div class=&quot;ContentWrapper-kdagst-0 fPLsyX&quot;&gt;
                    &lt;div class=&quot;FieldBaseWrapper-sc-14kmybr-0 cspqsP&quot;&gt;
                        &lt;div class=&quot;Content__ContentWrapper-ve26fj-2 feWTNh&quot;&gt;
                            &lt;div class=&quot;Content__ChildWrapper-ve26fj-0 ilJUcG&quot;&gt;
                                &lt;div class=&quot;Content-ve26fj-1 llCsQv&quot;&gt;
                                    &lt;div class=&quot;ReadViewContentWrapper-xymwx5-0 kLiHRY&quot;&gt;&lt;span
                                            class=&quot;sc-hmzhuo igqoCl&quot;&gt;8&lt;/span&gt;
                                        &lt;button class=&quot;EditButton-sc-1v6bv8a-0 doKTJP&quot; aria-label=&quot;Edit&quot;
                                                type=&quot;button&quot;&gt;&lt;/button&gt;
                                    &lt;/div&gt;
                                &lt;/div&gt;
                            &lt;/div&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div&gt;
        &lt;div class=&quot;sc-LAuEU kNfSUt&quot;&gt;&lt;h2 class=&quot;sc-hrBRpH juzrVt&quot;&gt;Reporter&lt;/h2&gt;&lt;/div&gt;
        &lt;div class=&quot;sc-eZXMBi iWQVSp&quot; data-test-id=&quot;issue.views.field.user.reporter&quot;&gt;
            &lt;div class=&quot;RootWrapper-sc-1va80k6-0 fsVJT&quot;&gt;
                &lt;div style=&quot;position: absolute;&quot;&gt;&lt;label class=&quot;Label__LabelWrapper-sc-17towfw-0 keOXhT&quot;&gt;
                    &lt;div class=&quot;Label__LabelInner-sc-17towfw-1 jbyPaz&quot;&gt;&lt;span&gt;Reporter&lt;/span&gt;&lt;/div&gt;
                &lt;/label&gt;&lt;/div&gt;
                &lt;div class=&quot;ContentWrapper-kdagst-0 fPLsyX&quot;&gt;
                    &lt;div class=&quot;FieldBaseWrapper-sc-14kmybr-0 cspqsP&quot;&gt;
                        &lt;div class=&quot;Content__ContentWrapper-ve26fj-2 feWTNh&quot;&gt;
                            &lt;div class=&quot;Content__ChildWrapper-ve26fj-0 ilJUcG&quot;&gt;
                                &lt;div class=&quot;Content-ve26fj-1 llCsQv&quot;&gt;
                                    &lt;div class=&quot;ReadViewContentWrapper-xymwx5-0 kLiHRY&quot;&gt;
                                        &lt;div class=&quot;sc-iBlNuT fGlWRy&quot;&gt;
                                            &lt;div class=&quot;sc-iXKTDd ferkkY&quot;&gt;
                                                &lt;div data-test-id=&quot;profilecard-next.ui.profilecard.profilecard-trigger&quot;&gt;
                                                    &lt;div class=&quot;sc-kiXyGy iYsZzI&quot;&gt;
                                                        &lt;div class=&quot;sc-ekQYnd gFYdNG&quot;&gt;
                                                            &lt;div style=&quot;display: inline-block; position: relative; outline: 0px;&quot;&gt;
                                                                &lt;span class=&quot;css-1c24z73&quot;&gt;&lt;span class=&quot;css-zk1cl3&quot;
                                                                                                role=&quot;img&quot;
                                                                                                aria-label=&quot;&quot;&gt;&lt;/span&gt;&lt;/span&gt;
                                                            &lt;/div&gt;
                                                        &lt;/div&gt;
                                                        &lt;div class=&quot;SingleLineTextInput__ReadView-sc-4hfvq0-0 epXzqq&quot;&gt;
                                                            Test test
                                                        &lt;/div&gt;
                                                    &lt;/div&gt;
                                                &lt;/div&gt;
                                            &lt;/div&gt;
                                        &lt;/div&gt;
                                        &lt;button class=&quot;EditButton-sc-1v6bv8a-0 doKTJP&quot; aria-label=&quot;Edit Reporter&quot;
                                                type=&quot;button&quot;&gt;&lt;/button&gt;
                                    &lt;/div&gt;
                                &lt;/div&gt;
                            &lt;/div&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div&gt;
        &lt;div id=&quot;releases-panel-container&quot; class=&quot;sc-jHsedM dDOmWr&quot; data-test-id=&quot;development.releases-glance.actions&quot;&gt;
            &lt;div class=&quot;sc-jeZOwl eFGvoQ&quot;&gt;&lt;h2 class=&quot;sc-buBCPq fOTSoR&quot;&gt;Releases&lt;/h2&gt;&lt;/div&gt;
            &lt;div class=&quot;sc-cPwpjd egqpOy&quot;&gt;&lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

I cannot understand what I'm doing wrong...

答案1

得分: 1

你至少有三个错误:

  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,我们可以尝试找到更好的定位方法。

  1. 可能是等待时间过短。

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:

  1. 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.

  1. 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.

  1. 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(&quot;div[class*=&#39;Content__ChildWrapper&#39;] small&quot;)

huangapple
  • 本文由 发表于 2020年10月14日 16:47:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/64349708.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定