找不到select元素
页面代码大概如下<div class="col-xs-12 col-sm-8">
<select id="sel-car" class="chosen-select col-xs-12 col-sm-6 selecloption" data-placeholder="请选择车型" name="product_id" style="display: none;">
<option value="1">请选择车型</option>
<option value="610301">MAZDA6 ATENZA 阿特兹</option>
</select>
<div id="sel_car_chosen" class="chosen-container chosen-container-single chosen-container-single-nosearch chosen-container-active" style="width: 381px;" title="">
<a class="chosen-single chosen-default" tabindex="-1">
<span>请选择车型</span>
<div>
<b/>
</div>
</a>
用 Select select=new Select(driver.findElement(By.id("sel-car")));
select.selectByValue("610301");
来查找提示Element is not currently visible and so may not be interacted with 图片 你用xpath定位,试试
WebElement element=driver.findElement(By.xpath("xpath表达式"));---id="sel-car" 的xpath路径
element.findElement(By.xpath("xpath表达式")).click();---value="610301"的xpath路径
WebElement sourceCategory = chrome.findElement( By.id("selSourceCategory")); //获取来源下拉框
sourceCategory.findElements(By.tagName("option")).get(1).click(); //选择option
分享一下selenium基础学习博客:http://blog.csdn.net/lily_xl 选择select的option有以下三种方法
selectByIndex(int index) 通过index
selectByVisibleText(String text) 通过匹配到的可见字符
selectByValue(String value) 通过匹配到标签里的value
WebElement selector = driver.findElement(By.id("Selector"));
Select select = new Select(selector);
select.selectByIndex(3);
select.selectByVisibleText("桃子");
select.selectByValue("apple"); 掉渣饼 发表于 2016-6-23 22:38
你用xpath定位,试试
WebElement element=driver.findElement(By.xpath("xpath表达式"));---id="sel-car" ...
试了一下还是不行 oukangjun 发表于 2016-6-24 00:41
WebElement sourceCategory = chrome.findElement( By.id("selSourceCategory")); //获取来源下拉框
sou ...
WebElement sourceCategory = driver.findElement( By.id("sel-car"));
sourceCategory.findElements(By.tagName("option")).get(1).click();
还是报错 我下面的帖子有页面的图片 页面图片截图 chuodl 发表于 2016-6-27 10:52
试了一下还是不行
把报错信息发下吧,发完整些 掉渣饼 发表于 2016-6-27 12:53
把报错信息发下吧,发完整些
Caused by: org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
Build info: version: '2.45.0', revision: '5017cb8', time: '2015-02-26 23:59:50'
System info: host: 'USER-20160219JF', ip: '192.168.15.92', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_04'
Driver info: driver.version: unknown
at <anonymous class>.fxdriver.preconditions.visible(file:///C:/Users/ADMINI~1/AppData/Local/Temp/anonymous6681953886405693411webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:9587)
at <anonymous class>.DelayedCommand.prototype.checkPreconditions_(file:///C:/Users/ADMINI~1/AppData/Local/Temp/anonymous6681953886405693411webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12257)
at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/ADMINI~1/AppData/Local/Temp/anonymous6681953886405693411webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12274)
at <anonymous class>.DelayedCommand.prototype.executeInternal_(file:///C:/Users/ADMINI~1/AppData/Local/Temp/anonymous6681953886405693411webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12279)
at <anonymous class>.DelayedCommand.prototype.execute/<(file:///C:/Users/ADMINI~1/AppData/Local/Temp/anonymous6681953886405693411webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12221)
用的写法是
WebElement element = driver.findElement( By.xpath(".//select[@id='sel-car']"));
element.findElements(By.tagName("option")).get(1).click();
多谢了 我怎看你的截图,请选择车型是个span?不是select啊 不好意思我看错地方了,报错不是nosuchelement证明是找到了,只是不可见或不可操作,是不是你们页面有什么异步的操作?页面加载完了这个droplist还是不能操作的,要等什么动作之后才可以点击选择? display:none,这个样式让select不显示,所以报元素不可见
页:
[1]