1

I am using the following code using Java and Selenium:

public static void main(String[] args){
    WebDriver driver;
    DesiredCapabilities caps; 
    caps = new DesiredCapabilities();
    caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
            "lib/phantomjs.exe"); 
    caps.setBrowserName(DesiredCapabilities.phantomjs().getBrowserName());
    driver = new PhantomJSDriver(caps);
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    driver.get("https://www.cdp.net/en-US/Pages/CDPAdvancedSearchResults.aspx?k=microsoft");
    WebElement element = driver.findElement(By.className("ms-vb2"));
    String text = element.getText();
    String href = element.getAttribute("href");
    driver.manage().deleteAllCookies(); 
    driver.quit();
    System.out.println(text + " " + href);  
}

The specific portion of the page I am trying with the code contains the following. I am trying to extract the href from class ms-vb2, which is https://www.cdp.net/en-US/Results/Pages/Company-Responses.aspx?company=11930:

<td class="ms-vb2"><a href="https://www.cdp.net/en-US/Results/Pages/Company-Responses.aspx?company=11930">Microsoft Corporation</a><br/>USA</td>

I am getting the text but I am not getting the href. How can I extract that?

1 Answer 1

1

driver.findElement(By.className("ms-vb2")) would actually match the td element:

<td class="ms-vb2"><a href="https://www.cdp.net/en-US/Results/Pages/Company-Responses.aspx?company=11930">Microsoft Corporation</a><br>USA</td>

and it does not have an href attribute.

You need to locate the link inside. I would make a CSS selector:

driver.findElement(By.cssSelector(".ms-vb2 > a"))

Here we are searching for an a element directly inside the element having ms-vb2 class.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.