由于javascript回调,Selenium未选择select选项
我已经通读了stackoverflow上类似问题的答案,但没有一个答案适合我。大多数答案的具体区别在于select元素中的javascript回调。我已经尝试过选择对象,使用索引、值和文本的定位器,没有一个会选择正确的选项,我相信这都是由于javascript回调 这是我试图从中选择的元素:由于javascript回调,Selenium未选择select选项,java,selenium,xpath,selenium-webdriver,Java,Selenium,Xpath,Selenium Webdriver,我已经通读了stackoverflow上类似问题的答案,但没有一个答案适合我。大多数答案的具体区别在于select元素中的javascript回调。我已经尝试过选择对象,使用索引、值和文本的定位器,没有一个会选择正确的选项,我相信这都是由于javascript回调 这是我试图从中选择的元素: <select name="ctl0" onchange="javascript:setTimeout('__doPostBack(\'ctl0\',\'\')', 0)" id="ctl0" sty
<select name="ctl0" onchange="javascript:setTimeout('__doPostBack(\'ctl0\',\'\')', 0)" id="ctl0" style="width:205px;margin-left:30px;">
<option value="0">Option 0</option>
<option selected="selected" value="1">Option 1</option>
<option value="2">Option 2</option>
</select>
结果无差异的替代方法:
driver.findElement(By.id("ctl0")).click();
Select select = new Select(driver.findElement(By.id("ctl0")));
WebElement elem = select.getOptions().get(1);
System.out.println(elem.getText());
elem.click();
在回答之前,根据其他人的说法,我必须同时单击这两个对象,因为回调似乎愚弄了一个Select对象,从而愚弄了显式的click和XPath定位器
选择选择控件在这两种情况下都有效,选择选项似乎有效,但单击不会导致选择它
我尝试了下面的javascript选项,同样的问题
WebElement el = driver.findElement(By.id("ctl0"));
String jsScript = "showDropdown = function (element) "
+ "{"
+ " var event; "
+ " event = document.createEvent('MouseEvents'); "
+ " event.initMouseEvent('mousedown', true, true, window, 1, 0,0,0,0,false,false,false,false,0,null); "
+ " element.dispatchEvent(event); "
+ "}; "
+ "showDropdown(arguments[0]);";
((JavascriptExecutor)driver).executeScript(jsScript,el);
WebElement elem = el.findElement(By.xpath(".//option[@value = '1']"));
System.out.println("Option visible text is " + elem.getText());
elem.click();
该网站不是公开的,我无法控制它。请注意,下面的解决方案中指定的initMouseEvent没有所有必需的参数。我想我是对的。initMouseEvent现在也不推荐使用
我最初使用Selenium IDE记录操作。以下是它为Java导出的方式:
new Select(driver.findElement(By.id("ctl0"))).selectByVisibleText("Option 1");
driver.findElement(By.cssSelector("option[value=\"1\"]")).click();
在阅读本条后—
我尝试了这一点(也尝试了不带窗口前缀的函数):
但请记住:
org.openqa.selenium.WebDriverException: TypeError: window.__doPostBack is not a function (WARNING: The server did not provide any stacktrace information)
Ideas?Selenium提供了一个类,通过以下方法处理下拉列表:
WebElement el = driver.findElement(By.id("ctl0"));
Select select = new Select(el);
- 使用方法如下:
select.selectByIndex(1);
select.selectByValue("1");
select.selectByVisibleText("Option 1");
- 使用方法如下:
select.selectByIndex(1);
select.selectByValue("1");
select.selectByVisibleText("Option 1");
- 使用方法如下:
select.selectByIndex(1);
select.selectByValue("1");
select.selectByVisibleText("Option 1");
。单击()
已编辑:如果不幸上述方法不起作用,您可以尝试使用JavascriptExecutor
,如下所示:-
WebElement el = driver.findElement(By.id("ctl0"));
((JavascriptExecutor)driver).executeScript("showDropdown = function (element) {var event; event = document.createEvent('MouseEvents'); event.initMouseEvent('mousedown', true, true, window); element.dispatchEvent(event); }; showDropdown(arguments[0]);",el);
el.findElement(By.xpath(".//option[@value = '1']").click();
您是否尝试过使用Select
class??如果你尝试过,你也能分享吗?你能在公共URL上复制这个问题吗?是的,我完全知道这一点,正如我在最初的问题中所说的。但是Select方法也不起作用,如我更新的问题中所述。@csDave Ok您可以尝试使用JavascriptExecutor
然后..尝试更新的答案并告诉我我尝试过,没有区别。也许是Ubuntu上的Firefox才是问题所在?