Ivan Garrido
Ivan Garrido

Reputation: 67

Xpath not working with selenium Web Driver

I have a problem triying to recognize a xpath from the following web page

http://smartchanneltech.com/top100canalti/

This is the element I want to recognize: https://i.sstatic.net/q1ccv.jpg

This is the xpath that I´m using:/html/body/div/div/div[1]/h1/a

This is the code I´m using:

public WebElement Empresa (WebDriver driver, int Iterator) {

    //WebDriverWait wait = new WebDriverWait(driver, 10);

    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div/div/div[1]/h1/a")));
    return driver.findElement(By.xpath("/html/body/div/div/div[1]/h1/a"));

    }

And, finally, this is the error log: https://i.sstatic.net/hRlPG.jpg

I tried just to driver.findElement(By.xpath("/html/body/div/div/div[1]/h1/a")); but is not working also.

Can you help me with this please?

Upvotes: 2

Views: 190

Answers (3)

Mahmud Riad
Mahmud Riad

Reputation: 1165

I have update the method as below. Please use this.

public WebElement Empresa (WebDriver driver, int Iterator) {

//WebDriverWait wait = new WebDriverWait(driver, 10);

wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[@src='https://www.rise.global/display/top100-canalti/latest/embeddable/cut/stripes']")));

driver.switchTo().frame(driver.findElement(By.tagName("iframe")));

WebElement elem = driver.findElement(By.className("lb-leaderboard-widget-wrapper"));
WebElement elemH1 = elem.findElement(By.tagName("h1"));
WebElement elemIWant = elem.findElement(By.tagName("a"));

System.out.println(elemIWant.getAttribute("innerHTML").toString());

return elemIWant;

}

let me know your feedback.

Upvotes: 0

undetected Selenium
undetected Selenium

Reputation: 193058

If you look into the HTML DOM the WebElement is within an <iframe>. So we need to switch to the <iframe> first with proper WebDriverWait and then locate the WebElement as follows :

new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[@src='https://www.rise.global/display/top100-canalti/latest/embeddable/cut/stripes']")));
return driver.findElement(By.xpath("div[@class='lb-leaderboard-header']/h1/a[@class='lb-leaderboard-name']"));

Upvotes: 0

Murthi
Murthi

Reputation: 5347

It is inside a iframe. First switch to the frame and try identifying it.

public WebElement Empresa (WebDriver driver, int Iterator) {
   driver.switchTo().frame(0);
   String xpath="/html/body/div/div/div[1]/h1/a";
   wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpath)));
   return driver.findElement(By.xpath(xpath));
}

Upvotes: 1

Related Questions