简体   繁体   中英

Selenium Webdriver - Able to locate elements through firebug, but getting nosuchelementexception at Webdriver

What happens is that I have this webpage: 主1 The following div is what's visible in cyan in the screenshot above. I've circled the bit of text I'm trying to find: 在此处输入图片说明

I've tried Xpath and CSS. They both work in Firebug and they both locate the elements fine inside this floating div. But when I run my script, I get the nosuchelement exception.

-Things I'm suspicious of:

  1. As you can see, this div is a call to another webpage (indicated by the whole html tree inside the div). This might be the reason Webdriver goes to hell and doesn't find anything. But I have no idea how to tell it to look in this window.
  2. The div acts like a floating pop-up, graying out the rest of the background page. Not sure if that has anything to do, but it might be causing trouble too.

I don't think it's necessary, but here is one of the CSS locators that I'm using in Firepath and works in locating the 5040 text.

driver.findElement(By.cssSelector(".feature>tbody>tr>td[width='312']")

So the question here is: how do I locate this text in these td 's if my xpath/css locators work manually but not with WebDriver?

See that iframe element - it is the reason you are getting the exception.

You need to switch to iframe before finding an element:

driver.switchTo().frame("Content")

Or (in case it is not working due to spaces in the id ):

driver.switchTo().frame(driver.findElement(By.cssSelector("iframe[id$=Content]"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM