简体   繁体   English

单击Selenium with Java for Internet Explorer中的单选按钮

[英]Clicking a radio button in Selenium with Java for Internet Explorer

My code runs perfectly on FF and Chrome, the IE driver starts but the radio button is not clicked: 我的代码在FF和Chrome上完美运行,IE驱动程序启动,但未单击单选按钮:

    WebElement radio = driver.findElement(By.xpath("//input[@value='5'][@name='rPay']/following-sibling::span"));
    System.out.println(radio.isDisplayed());
    radio.click();
    System.out.println(radio.isSelected());

The output I get is true, false, meaning that the element was visible but not clicked. 我得到的输出是true,false,表示该元素可见但未被单击。 Here is the html code I am running my test on: 这是我在其上运行测试的html代码:

<input type="radio" onclick="CheckPayment(document.frmWS,'');" value="5"    name="rPay" style="position: absolute; left: -9999px;">
<span class="custom-radio"></span>

I actually have to click on the span tag, not on the input tag. 实际上,我必须单击span标签,而不是input标签。 In Firefox and Chorme it won't work the other way around. 在Firefox和Chorme中,它将无法正常工作。

Please include below capabilities before you call IEDriver. 在致电IEDriver之前,请包括以下功能。

DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();

ieCapabilities.setCapability("nativeEvents", false);    
ieCapabilities.setCapability("unexpectedAlertBehaviour", "accept");
ieCapabilities.setCapability("ignoreProtectedModeSettings", true);
ieCapabilities.setCapability("disable-popup-blocking", true);
ieCapabilities.setCapability("enablePersistentHover", true);

driver = new InternetExplorerDriver(ieCapabilities);

isDisplayed() and isSelected() is what making this difference. isDisplayed()isSelected()是造成这种差异的原因。 Lets try below and it should work. 让我们在下面尝试,它应该可以工作。

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement radio = wait.until(ExpectedConditions.elementToBeSelected(By.xpath("//input[@name='rPay']//span[@class='custom-radio']")));

Use below code :- 使用以下代码:-

WebElement element= driver.findElement(By.xpath("//input[@value='5'][@name='rPay']/following-sibling::span"));

JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);

OR Try this 或尝试这个

driver.findElement(By.xpath("//input[@value='5'][@name='rPay']/following-sibling::span")).sendKeys(Keys.ENTER);

I hope it will help you :) 希望对您有所帮助:)

Please get back to me if still facing any issue :) 如果仍然遇到任何问题,请与我联系:)

After searching for the whol afternoon I found what was missing. 搜索了整个下午之后,我发现了所缺少的东西。 My code for the IE web driver was this: 我的IE Web驱动程序代码是这样的:

    DesiredCapabilities cap = new DesiredCapabilities();
    cap.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
    cap.setCapability(InternetExplorerDriver.ELEMENT_SCROLL_BEHAVIOR, true);
    cap.internetExplorer().setCapability("ignoreProtectedModeSettings", true);
    cap.setCapability("IE.binary", "C:/Program Files (x86)/Internet Explorer/iexplore.exe");
    cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
    cap.setJavascriptEnabled(true);
    cap.setCapability("requireWindowFocus", true);
    cap.setCapability("enablePersistentHover", false);
    System.setProperty("webdriver.ie.driver", System.getProperty("user.dir")+"\\webdriver\\iedriverserver.exe");
    WebDriver driver = new InternetExplorerDriver(cap);

After adding one line, I found somewhere on the web, it worked fine: 添加一行之后,我在网上找到了某个地方,它工作正常:

cap.setCapability("nativeEvents", false);

Thanks a lot for everybodies help anyhow! 非常感谢大家的帮助!

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

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