Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium:使用java选择一个div元素_Java_Html_Eclipse_Selenium - Fatal编程技术网

Selenium:使用java选择一个div元素

Selenium:使用java选择一个div元素,java,html,eclipse,selenium,Java,Html,Eclipse,Selenium,我必须使用selenium在下面的代码中选择“模块名称”,有人能帮我吗 <iframe width="142" height="225" frameborder="0" disabled="true" src="blank.htm" scrolling="no"> <div style="position: absolute; visibility: inherit; overflow: hidden; cursor: default; color: rg

我必须使用selenium在下面的代码中选择“模块名称”,有人能帮我吗

<iframe width="142" height="225" frameborder="0"
        disabled="true" src="blank.htm" scrolling="no">
   <div style="position: absolute; visibility: inherit; overflow: hidden; cursor: default; color: rgb(0, 86, 143); background-color: rgb(237, 238, 240); text-align: left; width: 139px; height: 14px; padding-left: 1px; padding-top: 1px; left: 1px; top: 1px;">
       Module Name
   </div>
谢谢大家,谢谢你宝贵的建议。但是,仍然无法点击。是否有任何兼容性或安装问题会影响这一点?? 请查看输出以更好地理解问题

获得如下输出:

Started InternetExplorerDriver server (32-bit) 
2.44.0.0 
Listening on port 41280 
log4j:WARN No appenders could be found for logger (org.apache.http.client.protocol.RequestAddCookies).
log4j:WARN Please initialize the log4j system properly.
Latest
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with xpath == /html/body/div[8]/div[1] (WARNING: The server did not provide any stacktrace information)

由于要单击的div位于
iframe
中,因此需要首先切换到它。不幸的是,框架没有
id
,您需要在页面上显示(以零为基础)。不幸的是,除了内容之外,div似乎没有任何显著的特性。下面是一个简化的
findElement

driver.switchTo().frame(0);
driver.findElement(By.xpath("//div[contains(., 'Module Name')]")).click();

我已假定页面上的第一个
帧/iframe
,请进行相应调整。

根据您的要求为我运行以下代码:

 driver.switchTo().frame(0);
 String data = driver.findElement(By.xpath("/html/body/div[20]/div[2]")).getText().toString();
 System.out.println(data);
输出将为:模块名称


注意:如果选择了不正确的xpath,则可以更正。如果需要,也可以替换框架索引。

根据您提供的
html
代码片段,我怀疑框架本身已被禁用,无法接受任何UI交互。为了与框架交互,还需要执行一些其他操作来启用框架(可能是单击某个对象)。如果使用有效的选择器启用该选项,则可以执行单击

driver.switchTo().frame(0);
driver.findElement(By.xpath("//div[".='Module Name'"]).click();

在这种情况下,您还需要确保target
frame
是索引为
0
的正确目标。或者找到如何使用
xpath

识别
frame
您是否尝试过:driver.findelelement(By.xpath(“/html/body/div[20]/div[2]”)。单击();无需使用“DIV”标记来查找元素,请参阅我的答案。我发布的代码将帮助您获取模块名称。我尝试了上面的一个,直到不起作用。不是这样。请按照我的回答代码进行尝试:driver.switchTo().frame(0);String data=driver.findElement(By.xpath(“/html/body/div[20]/div[2]”);系统输出打印项次(数据);尝试过但得到“无接触异常”
driver.switchTo().frame(0);
driver.findElement(By.xpath("//div[".='Module Name'"]).click();