Home > Back-end >  Selenium WebDriver get text inside of a span
Selenium WebDriver get text inside of a span

Time:08-13

I'm trying to extract the "This is our address" text inside of a span where HTML code are as follows:

<div id="comp-kvi6khho" >
<p >
<span style="letter-spacing:normal;">
<span >This is our address</span>

I have tried:

driver.findElement(By.className("color_11")).getText();
driver.findElement(By.cssSelector("#comp-kvi6khho > p:nth-child(1) > span:nth-child(1) > span:nth-child(1)")).getText();
driver.findElement(By.xpath("/html/body/div[1]/div/div[3]/div/main/div/div/div/div[2]/div/div/div/section[9]/div[2]/div[1]/div[2]/div/div[4]/p/span/span")).getAttribute("InnerHTML");

so far. But all of them I get Unable to locate element error. How can I extract the text inside of most inner span class?

CodePudding user response:

Ok so I was not aware of the existence of iframes. The element I was looking for is inside of an iframe. First, I had to locate the iframe, then switch to it and then I located the element itself.

The code I used for that is below:

driver.switchTo().frame("iframe").findElement(By.cssSelector("#comp-kvi6khho > p:nth-child(1) > span:nth-child(1) > span:nth-child(1)")).getText();
  • Related