Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.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 WebDriver和xpath位于WebElement内部_Selenium_Xpath - Fatal编程技术网

Selenium WebDriver和xpath位于WebElement内部

Selenium WebDriver和xpath位于WebElement内部,selenium,xpath,Selenium,Xpath,我有一个包含多个表单的页面,表单有自己的提交按钮和其他元素。在测试页面时,我找到了第二个表单 WebElement form = getDriver().findElement(By.id("form2")); 然后单击“字段”和“提交”按钮 form.findElement(By.name("text")).sendKeys("Adding some text here"); form.findElement(By.xpath("//input[@type='submit']")).clic

我有一个包含多个表单的页面,表单有自己的提交按钮和其他元素。在测试页面时,我找到了第二个表单

WebElement form = getDriver().findElement(By.id("form2"));
然后单击“字段”和“提交”按钮

form.findElement(By.name("text")).sendKeys("Adding some text here");
form.findElement(By.xpath("//input[@type='submit']")).click();
但是,这些xpath位置在第一种形式上生效。xpath真的不能在指定元素中工作吗?

请尝试相对路径:

form.findElement(By.xpath(".//input[@type='submit']")).click();

事实上,Selenium使用xpath元素找到的第一个元素。如果您知道确切的订单号,则可以将此编号添加到xpath
//输入[@type='submit'][2]
。请注意,xpath中的编号从1开始,而不是从0开始。因此,给定的xpath将为您找到带有
@type='submit'

的第二个输入,这是不正确的。只有当
元素是同一元素的直接子元素时,它才会起作用,因此,如果它对OP起作用,那么它只会意外地起作用。