我有一些Selenium Web驱动程序代码,无法识别第23行和第81行之间的任何Web元素。但是,我能够识别第23行和第81行以下的所有Web元素并与之交互。
19 ….
20 <div id="main" class="well content" ui-view="content">
21 <iframe src="swagger-ui/index.html" frameborder="0" marginheight="0" marginwidth="0"
22 width="100%" height="900" scrolling="auto" target="_top" title="Swagger UI">
23 #document
24 <!doctype html>
25 <html>
26 <head>…</head>
27 <body class= “swagger-section” marginWidth= “0” marginheight= “0”>
<div class = “swagger-ui-wrap”>
<a id =”logo” href=http://swagger.io>swagger</a>
…
</div>
80 </body>
81 </iframe>
82 </div>
83 ….
我已将以下Maven依赖项添加到我的POM中:
<!-- https://mvnrepository.com/artifact/io.swagger/swagger-core -->
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-core</artifactId>
<version>1.5.21</version>
</dependency>
有人可以让我知道我是否需要在POM中添加其他依赖项,或者在下面的语句中提供一些关于为什么size = 0的见解?
size = driver.findElements(By.xpath("//*[@id='swagger-ui-container']")).size();
答案 0 :(得分:1)
SeleniumWebDriver 无法将第23行和第81行之间的任何Web元素标识为这些行:
21 <iframe src="swagger-ui/index.html" frameborder="0" marginheight="0" marginwidth="0"
22 width="100%" height="900" scrolling="auto" target="_top" title="Swagger UI">
23 #document
24 <!doctype html>
25 <html>
26 <head>…</head>
27 <body class= “swagger-section” marginWidth= “0” marginheight= “0”>
<div class = “swagger-ui-wrap”>
<a id =”logo” href=http://swagger.io>swagger</a>
…
</div>
80 </body>
81 </iframe>
在<iframe>
之内。
要识别<iframe>
标记内的所有WebElement并与之交互,您必须:
您可以使用以下任一解决方案:
cssSelector :
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[title='Swagger UI'][src='swagger-ui/index.html']")));
my_size = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("#swagger-ui-container"))).size();
xpath :
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[@title='Swagger UI' and @src='swagger-ui/index.html']")));
my_size = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//*[@id='swagger-ui-container']"))).size();
在这里您可以找到有关Ways to deal with #document under iframe的相关讨论