简体   繁体   English

Selenium Webdriver无法通过xPath定位元素

[英]Selenium Webdriver can not locate element by xPath

I am using two different ways to access a web element with Selenium webdriver (JavaScript) . 我正在使用两种不同的方式通过Selenium webdriver (JavaScript)访问Web元素。

The first way uses a number indicating third div element in the parent div . 第一种方法使用一个数字,指示父div中的第三个div元素。 The code is like this: 代码是这样的:

driver.findElement(By.xpath("//div[@id='sld-layer-container']/div/div/ul/li[2]/div/div[2]/div/div[3]/select/option[2]")).click();

This code doesn't work. 此代码无效。 It returns error: ElementNotVisibleError: element not visible: Element is not currently visible and may not be manipulated 它返回错误: ElementNotVisibleError: element not visible: Element is not currently visible and may not be manipulated

The second way uses class to identify the specific div in parent div . 第二种方法使用类来确定具体divdiv Code is like this: 代码是这样的:

driver.findElement(By.xpath("//div[@id='sld-layer-container']/div/div/ul/li[2]/div/div[2]/div/div[@class = 'col-md-5']/select/option[2]")).click();

As you can see, the only difference is the identifier of last div element in xPath string. 如您所见,唯一的区别是xPath字符串中最后一个div元素的标识符。 They should indicate the same thing. 他们应该指出同一件事。 Magically the second one works but not the first one. 神奇的是第二个有效,但第一个无效。

Here is a screenshot of css elements. 这是css元素的屏幕截图。 The div highlighted is what I am trying to locate. 突出显示的div是我要查找的位置。 在此处输入图片说明

Can anyone help me with this? 谁能帮我这个?

Update 1: 更新1:

As @Mahipal and @iamkenos required, I expended the div and now it is showing select and option . 按照@Mahipal和@iamkenos的要求,我花费了div ,现在它显示了selectoption I thought the issue was only caused by not being able to locate the div but it seems not. 我认为问题仅是由于无法找到div引起的,但似乎并非如此。 Please help further. 请进一步帮助。 在此处输入图片说明

you can try as below: 您可以尝试如下操作:

 Select select = new Select (driver.findElement(By.xpath("//div[@id='sld-layer-container']/div/div/ul/li[2]/div/div[2]/div/div[3]/select")));

 select.selectByVisibleText("PROJECT_VALUE");

您也可以在xpath下面尝试...

//div[@id='featureClassList']//div[@class='col-md-5']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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