无法与元素互动

时间:2020-07-07 09:32:44

标签: selenium selenium-webdriver

从以下HTML代码段中,我必须单击值为Same as user name的单选按钮

<fieldset class="setPwd" style="padding:0.769rem 1.15rem;">
        <legend>Set Password </legend>
        <input type="radio" name="scheme" id="New" value="Default" checked="">Default
        <input type="radio" name="scheme" id="Username" value="Same as user name">Same as user name
        <br><br>
        <input type="text" style="display:none">
        <input type="password" onpaste="return false" ondrop="return false" id="password" name="password" maxlength="40" autocomplete="off" style="width:66%;padding:0.00rem;">
</fieldset>

因此,点击它-

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

但这会导致错误-

org.openqa.selenium.ElementNotInteractableException: element not interactable
  (Session info: chrome=83.0.4103.116)
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'DT01', ip: '192.16', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_172'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 83.0.4103.116, chrome: {chromedriverVersion: 83.0.4103.39 (ccbf011cb2d2b..., userDataDir: C:\Users\gajanan\AppData\Lo...}, goog:chromeOptions: {debuggerAddress: localhost:54490}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:virtualAuthenticators: true}
Session ID: eedc94ffa7211db6c7252bb85421992a

    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:548)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:276)
    at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:83)
    at org.test.selenium.FramTestBhanu.highlightParentElementTest(FramTestBhanu.java:54)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:73)
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)

在考虑元素的html代码片段时,是否有一些我应该更正的方法?

注意,我可以手动单击元素。

4 个答案:

答案 0 :(得分:0)

您应该等待该元素可见

 if (!driver.findElement(By.id("Username")).isSelected()) {
        driver.findElement(By.id("Username")).click();
    }

使用此代码段

答案 1 :(得分:0)

尝试使用以下css选择器:

driver.findElement(By.cssSelector("fieldset.setPwd input#Username")).click();

答案 2 :(得分:0)

我花了一个小时左右就可以弄清楚。首先,我移到该元素,然后像-

一样单击它
Actions actions = new Actions(driver);
actions.moveToElement(driver.findElement(By.id("Username")));
actions.click().build().perform();

答案 3 :(得分:0)

我认为,您的XPath很好,答案中提供的其他XPath也可以使用,但问题是您需要添加一些等待元素可见或可单击,如果您不使用任何类型的等待,那么硒将去尝试仅查找一次元素,并且不会重复该过程。

  1. 您可以亲自使用隐式等待或显式等待,我建议您使用显式等待
WebDriverWait wait=new WebDriverWait(driver,10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("Username)));

//above lines wait for the element to be visible 

//once it is visible you can click on that 

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

PS:显式等待的好处在于,在给定的时间中,它将等待元素最大值,因为在我们的情况下,它是15,是可见的元素,在此之前它不会等待,直到隐式等待它才会等待完整时间提到