无法单击单选按钮Internet Explorer

时间:2016-05-31 07:42:57

标签: java internet-explorer selenium selenium-webdriver

我无法在IE浏览器中使用Selenium网络驱动程序点击单选按钮。

html如下:

<TD class=PlainText>
<INPUT onclick="javascript: ShowHideInvoicePanel();setTimeout('__doPostBack(\'ctl00$cphClaimFlow$tabcontainerClaimFlow$tabFulfillment$Shipping$rdoYesFindMyPhone\',\'\')', 0)" id=ctl00_cphClaimFlow_tabcontainerClaimFlow_tabFulfillment_Shipping_rdoYesFindMyPhone type=radio value=rdoYesFindMyPhone name=ctl00$cphClaimFlow$tabcontainerClaimFlow$tabFulfillment$Shipping$phoneanswer>
<LABEL for=ctl00_cphClaimFlow_tabcontainerClaimFlow_tabFulfillment_Shipping_rdoYesFindMyPhone>Yes</LABEL>
<INPUT onclick="javascript: ShowHideInvoicePanel();setTimeout('__doPostBack(\'ctl00$cphClaimFlow$tabcontainerClaimFlow$tabFulfillment$Shipping$rdoNoFindMyPhone\',\'\')', 0)" id=ctl00_cphClaimFlow_tabcontainerClaimFlow_tabFulfillment_Shipping_rdoNoFindMyPhone type=radio value=rdoNoFindMyPhone name=ctl00$cphClaimFlow$tabcontainerClaimFlow$tabFulfillment$Shipping$phoneanswer>
<LABEL for=ctl00_cphClaimFlow_tabcontainerClaimFlow_tabFulfillment_Shipping_rdoNoFindMyPhone>No</LABEL>

以下是我的代码:

WebDriverWait waitForErasePhnYesRadio=new WebDriverWait(driver, timeOut);
    WebElement elementErasePhnYesRadio= waitForErasePhnYesRadio.until(ExpectedConditions.elementToBeClickable(By.id("ctl00_cphClaimFlow_tabcontainerClaimFlow_tabFulfillment_Shipping_rdoYesFindMyPhone")));
    elementErasePhnYesRadio.click();
    logger.info("Clicked on Yes button of erase phone script");

    if(elementErasePhnYesRadio.isSelected()==false){
        System.out.println("is radio btn selected 1 : "+elementErasePhnYesRadio.isSelected());
        elementErasePhnYesRadio.click();
        System.out.println("is radio btn selected 2 : "+elementErasePhnYesRadio.isSelected());
    }
    System.out.println("is radio btn selected 3 : "+elementErasePhnYesRadio.isSelected());

我已经添加了等待声明,因为我正在等待单选按钮显示。代码正在执行,不会显示错误或异常。

下面的输出打印在控制台中:

INFO [main] (ShippingPage.java:45) - Clicked on Yes button of erase phone script
is radio btn selected 1 : false
is radio btn selected 2 : false
is radio btn selected 3 : false

2 个答案:

答案 0 :(得分:0)

我不会在引号中看到Ids .....请检查一下。

JavascriptExecutor js = (JavascriptExecutor)driver;
Js.executeScript("document.getElementById('ctl00_cphClaimFlow_tabcontainerClaimFlow_tabFulfillment_Shipping_rdoYesFindMyPhone').checked = true;");

使用JS执行器和See this link尝试上面的代码,selenium doc说,鼠标不应该在脚本执行时悬停,因为在IE中它完全依赖于本机事件。

最后有时会发生this

答案 1 :(得分:0)

driver.findElement(By.id("ctl00_cphClaimFlow_tabcontainerClaimFlow_tabFulfillment_Shipping_rdoYesFindMyPhone")).click();