0

I wrote a script and received a stale element error. I know what the problem is, but I don't know how to fix the script.

I am writing a script to do the following via FireFox: 1), Launch google.com, 2), type "pluralsight", 3), submit, 4), click images link.

I was able to perform every step except for select the images link. When I examined the error, it looks like the ide is searching for the images link located on the google home page rather than the images link that appears after hitting search on google (I hope that makes sense). Here is the script:

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

I appreciate the help. Thanks!

A-L
  • 9
  • 4
  • Hi @A-L, remove `driver.navigate().refresh();` line from your code which is not necessary and your code will work – Ali Mar 02 '19 at 18:52
  • I tried that. Unfortunately, it did not work – A-L Mar 04 '19 at 05:46
  • It is working perfectly fine for me, I don't know what wrong you have done? Can you try this `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();` – Ali Mar 04 '19 at 09:16
  • Can you share the full error log instead of the screenshot? – Ali Mar 04 '19 at 09:19
  • Full error listed in the answer below – A-L Mar 05 '19 at 00:37

1 Answers1

0

The image below is what I am trying to get:

Expected End Result

The actual result is as follows (without driver.navigate().refresh()):

Actual End Result 1

And with driver.navigate().refresh():

Actual End Result 2

Below is an error Log:

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)

With the above image, what ends up happening is after "pluralsight" is in the search bar, the page doesn't submit (meaning it doesn't search). Instead, it refreshes (resulting in the search bar emptying), than selects the "Images" link from the home page.

Ali
  • 1,689
  • 1
  • 5
  • 12
A-L
  • 9
  • 4