【问题标题】:JSON Status Mapping for Stale Element - Selenium and Java陈旧元素的 JSON 状态映射 - Selenium 和 Java
【发布时间】:2019-03-02 15:08:46
【问题描述】:

我编写了一个脚本并收到一个过时元素错误。我知道问题出在哪里,但我不知道如何修复脚本。

我正在编写一个脚本来通过 FireFox 执行以下操作:1)、启动 google.com、2)、输入“pluralsight”、3)、提交、4)、单击图片链接。

除了选择图像链接之外,我能够执行每个步骤。当我检查错误时,看起来 ide 正在搜索位于谷歌主页上的图片链接,而不是在谷歌上搜索后出现的图片链接(我希望这是有道理的)。这是脚本:

enter code here
package com.pluralsight;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class WebDriverTutorial {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        //System.setProperty("webdriver.gecko.driver", "C:\\Program Files(x86)\\Drivers\\Gecko\\geckodriver.exe");

        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.google.com");
        WebElement searchfield = driver.findElement(By.name("q"));

        searchfield.sendKeys("pluralsight");
        searchfield.submit();

        WebElement imagesLink= driver.findElements(By.linkText("Images")).get(0);
        driver.navigate().refresh();
        imagesLink.click();

Snapshot of script and error

感谢您的帮助。谢谢!

【问题讨论】:

  • 嗨@A-L,从您的代码中删除driver.navigate().refresh(); 行,这是不必要的,您的代码将正常工作
  • 我试过了。不幸的是,它没有用
  • 它对我来说工作得很好,我不知道你做错了什么?你能试试这个driver.get("http://www.google.com"); WebElement searchfield = driver.findElement(By.name("q")); searchfield.sendKeys("pluralsight"); searchfield.submit(); WebElement imagesLink= driver.findElements(By.linkText("Images")).get(0); imagesLink.click();
  • 你能分享完整的错误日志而不是截图吗?
  • 以下答案中列出的完整错误

标签: java selenium-webdriver staleelementreferenceexception


【解决方案1】:

下面的图片是我想要得到的:

Expected End Result

实际结果如下(不带driver.navigate().refresh()):

Actual End Result 1

还有driver.navigate().refresh():

Actual End Result 2

以下是错误日志:

1551745909286   mozrunner::runner   INFO    Running command: "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe" "-marionette" "-foreground" "-no-remote" "-profile" "C:\\Users\\ALILLI~1\\AppData\\Local\\Temp\\rust_mozprofile.riKtO8zAttRA"
1551745910784   addons.webextension.screenshots@mozilla.org WARN    Loading extension 'screenshots@mozilla.org': Reading manifest: Invalid host permission: resource://pdf.js/
1551745910785   addons.webextension.screenshots@mozilla.org WARN    Loading extension 'screenshots@mozilla.org': Reading manifest: Invalid host permission: about:reader*
1551745914653   Marionette  INFO    Listening on port 53795
Mar 04, 2019 6:31:54 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Mar 04, 2019 6:31:58 PM org.openqa.selenium.remote.ErrorCodes toStatus


**INFO: HTTP Status: '404' -> incorrect JSON status mapping for 'stale element reference' (400 expected)**
Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: **The element reference of <a class="gb_d" href="https://www.google.com/imghp?hl=en&tab=wi"> is stale; either the element is no longer attached to the DOM, it is not in the current frame context, or the document has been refreshed
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html**


Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
System info: host: 'DESKTOP-FHGCOLL', ip: '192.168.1.130', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '10.0.1'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{moz:profile=C:\Users\ALiLLiNOiS\AppData\Local\Temp\rust_mozprofile.riKtO8zAttRA, rotatable=false, moz:geckodriverVersion=0.24.0, timeouts={implicit=0.0, pageLoad=300000.0, script=30000.0}, pageLoadStrategy=normal, unhandledPromptBehavior=dismiss and notify, strictFileInteractability=false, moz:headless=false, platform=ANY, moz:accessibilityChecks=false, moz:useNonSpecCompliantPointerOrigin=false, acceptInsecureCerts=false, browserVersion=65.0.2, moz:shutdownTimeout=60000.0, platformVersion=10.0, moz:processID=14248.0, browserName=firefox, javascriptEnabled=true, platformName=windows, setWindowRect=true, moz:webdriverClick=true}]
Session ID: 5948bf51-173f-4bef-bd2d-341e39a87846
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.base/java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:150)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:115)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:45)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:637)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:272)
    at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:82)
    at com.pluralsight.WebDriverTutorial.main(WebDriverTutorial.java:24)

对于上面的图片,最终发生的事情是在搜索栏中出现“pluralsight”之后,页面没有提交(意味着它没有搜索)。相反,它会刷新(导致搜索栏清空),然后从主页中选择“图像”链接。

【讨论】:

    猜你喜欢
    • 2017-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2018-04-08
    相关资源
    最近更新 更多