线程“main”org.openqa.selenium.elementNotInteractiableException中的java异常:元素不可交互

ryevplcw  于 2021-07-08  发布在  Java
关注(0)|答案(2)|浏览(378)

我有以下代码:
导入java.util.hashmap;导入java.util.map;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;

public class LoginToThePortal {
     public static void main(String[] args) throws InterruptedException
     {

         System.setProperty("webdriver.chrome.driver","C:\\chromedriver\\chromedriver.exe");
         WebDriver driver=new ChromeDriver();
         driver.manage().window();
         driver.get("");
         Thread.sleep(2000);
         WebElement username=driver.findElement(By.id("sfdc_username_container"));
         Thread.sleep(5000);
         WebElement password=driver.findElement(By.id("sfdc_password_container"));
         WebElement login=driver.findElement(By.xpath("//*[@id=\"centerPanel\"]/div/div[2]/div/div[2]/div/div[3]/button/span"));
         username.sendKeys("");
         password.sendKeys("");

          login.click();

    }
    }

我收到以下错误:

Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: element not interactable.

我不明白我怎么会错。我要说的是我才刚开始(我还在学习)。有人能看一下吗?

qcbq4gxm

qcbq4gxm1#

您可以尝试使用下面更新的xpath吗?希望这能解决您的问题

System.setProperty("webdriver.chrome.driver","C:\\chromedriver\\chromedriver.exe");
    WebDriver driver=new ChromeDriver();
    driver.manage().window();
    driver.get("https://qa-advisor.cs105.force.com/s/login/");
    Thread.sleep(2000);
    WebElement username = driver.findElement(By.xpath
           ("//div[@id='sfdc_username_container']//child::div/input[1]"));
    Thread.sleep(5000);
    WebElement password = driver.findElement(By.xpath
           ("//div[@id='sfdc_password_container']//child::div/input[1]"));
    WebElement login = driver.findElement(By.xpath
           ("//button[@class='slds-button slds-button--brand loginButton uiButton- 
                         -none uiButton']//child::span"));
    username.sendKeys("test");
    password.sendKeys("test");
    login.click();

zsohkypk

zsohkypk2#

要将字符序列发送到username和password字段,您需要将webdriverwait用于 elementToBeClickable() 您可以使用以下基于css选择器的定位器策略:

driver.get("https://qa-advisor.cs105.force.com/s/login/");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div#sfdc_username_container input[placeholder='Username']"))).sendKeys("Test");
driver.findElement(By.cssSelector("div#sfdc_password_container input[placeholder='Password']")).sendKeys("Test");

浏览器快照:

相关问题