Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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
如何构造元素的Xpath_Xpath_Selenium_Selenium Webdriver_Selenium Ide - Fatal编程技术网

如何构造元素的Xpath

如何构造元素的Xpath,xpath,selenium,selenium-webdriver,selenium-ide,Xpath,Selenium,Selenium Webdriver,Selenium Ide,SeleniumIDE自动生成的Xpath不起作用,我的意思是脚本失败了,因为它找不到任何具有此类Xpath的元素 有没有办法手动生成Xpath <div id="ext-gen1252" class="x-trigger-index-0 x-form-trigger x-form-arrow-trigger x-form-trigger-last x-unselectable" role="button" style="-moz-user-select: none;"></d

SeleniumIDE自动生成的Xpath不起作用,我的意思是脚本失败了,因为它找不到任何具有此类Xpath的元素

有没有办法手动生成Xpath

<div id="ext-gen1252" class="x-trigger-index-0 x-form-trigger x-form-arrow-trigger x-form-trigger-last x-unselectable" role="button" style="-moz-user-select: none;"></div>
这是java/webdriver,因为它是从IDE导出的

请尽快给我一些解决方案


谢谢

是的,您可以创建,例如: 下面显示的是Html,然后是body,然后是div[]等等。。 它将像“Html/body/div”一样创建 您可以这样创建xpath

<document>
<html itemtype="http://schema.org/WebPage" itemscope="">
<head>
<body id="gsr" class="hp vasq" vlink="#61c" text="#222" link="#12c" bgcolor="#fff" alink="#dd4b39" onload="try{if(!google.j.b){document.f&&document.f.q.focus();document.gbqf&&document.gbqf.q.focus();}}catch(e){}if(document.images)new Image().src='/images/nav_logo170.png'">
<div id="viewport" class="ctr-p">

IDE和FirePath等工具只能在一定程度上帮助您。您问题中的xpath示例就是一个完美的例子。它不仅不起作用,而且很难猜测它的目标是什么元素。解决办法是什么?学习如何自己构造Xpath或CSS。网上有很多例子。这是给和的

至于你的问题,xpath应该是这样的

driver.findElement(By.xpath(".//div[@class='x-trigger-index-0']").click();
我强烈推荐使用xpath,其原因在Selenium社区中也有详细的文档记录

driver.findElement(By.cssSelector("div[class='x-trigger-index-0']").click();

firepath和xpath helper以及许多其他扩展只是基于id元素生成一个新的应用程序,这通常不是您想要的


如果您使用的是chrome,您可以尝试通过指向元素巧妙地生成xpath的方法

-1发布失败的代码、输入示例和预期结果。问题是如何构造元素的xpath。为什么会有人投票反对询问代码的问题呢?您可以自己生成元素的Xpath,但不一定要手动生成。我使用FirePath,它是FireFox浏览器的一个附加组件。使用它,您只需单击web元素,它将为您提供其第X页。不过,我建议您在Xpath上使用CSS选择器。
driver.findElement(By.cssSelector("div[class='x-trigger-index-0']").click();