0

My project is crashing with: Element info: {Using=xpath, value=//a[contains(text(), 'Books')]/@href}.

The output (crash):

thufir@dur:~/NetBeansProjects/com.toscrape.books.selenium$ 
thufir@dur:~/NetBeansProjects/com.toscrape.books.selenium$ gradle clean run

> Task :run
1545499644656   mozrunner::runner       INFO    Running command: "/usr/lib/firefox/firefox" "-marionette" "--headless" "-foreground" "-no-remote" "-profile" "/tmp/rust_mozprofile.t8NF7Id7Xl9i"
*** You are running in headless mode.
1545499645464   [email protected]     WARN    Loading extension '[email protected]': Reading manifest: Invalid host permission: resource://pdf.js/
1545499645467   [email protected]     WARN    Loading extension '[email protected]': Reading manifest: Invalid host permission: about:reader*
1545499649326   Marionette      INFO    Listening on port 39039
1545499649779   Marionette      WARN    TLS certificate errors will be ignored for this session
Dec 22, 2018 9:27:29 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
JavaScript warning: http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js, line 1: Using //@ to indicate sourceMappingURL pragmas is deprecated. Use //# instead
Dec 22, 2018 9:27:32 AM dur.bounceme.net.SeleniumBase.WelcomePage <init>
INFO: http://books.toscrape.com/
Exception in thread "main" org.openqa.selenium.WebDriverException: TypeError: Expected an element or WindowProxy, got: [object Attr href="index.html"]
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'dur', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.18.0-13-generic', java.version: '1.8.0_191'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities {acceptInsecureCerts: true, browserName: firefox, browserVersion: 64.0, javascriptEnabled: true, moz:accessibilityChecks: false, moz:geckodriverVersion: 0.23.0, moz:headless: true, moz:processID: 26227, moz:profile: /tmp/rust_mozprofile.t8NF7I..., moz:shutdownTimeout: 60000, moz:useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, pageLoadStrategy: normal, platform: LINUX, platformName: LINUX, platformVersion: 4.18.0-13-generic, rotatable: false, setWindowRect: true, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: ed555cc0-75b3-4647-8ff8-5f067395eeb3
*** Element info: {Using=xpath, value=//a[contains(text(), 'Books')]/@href}
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        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 dur.bounceme.net.SeleniumBase.WelcomePage.populateCatalogue(WelcomePage.java:26)
        at dur.bounceme.net.SeleniumBase.App.initSelenium(App.java:26)
        at dur.bounceme.net.SeleniumBase.App.main(App.java:14)

> Task :run FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':run'.
> Process 'command '/usr/lib/jvm/java-8-openjdk-amd64/bin/java'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 10s
4 actionable tasks: 3 executed, 1 up-to-date
thufir@dur:~/NetBeansProjects/com.toscrape.books.selenium$ 

Up to the xpath query everything seems fine:

package dur.bounceme.net.SeleniumBase;

import java.util.logging.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

class WelcomePage {

    private static final Logger LOG = Logger.getLogger(App.class.getName());
    private WebDriver webDriver = null;

    private WelcomePage() {
    }

    private WelcomePage(WebDriver webDriver) {
        this.webDriver = webDriver;
        LOG.info(webDriver.getCurrentUrl());
    }

    static WelcomePage init(WebDriver webDriver) {
        return new WelcomePage(webDriver);
    }

    void populateCatalogue() {
        WebElement webElement = webDriver.findElement(By.xpath("//a[contains(text(), 'Books')]/@href"));
        LOG.info(webElement.getText());
    }

}

How do I locate the WebElement for the "Books" link at:

http://books.toscrape.com/

I'm looking to grab the catalogue categories.

1 Answer 1

1

You should use .getAttribute("href"); so that your code would look like:

WebElement webElement = webDriver.findElement(By.xpath("//a[contains(text(), 'Books')]" );
LOG.info(webElement.getAttribute("href"));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.