元素不可交互异常在硒 Web 自动化中



在下面的代码中,我无法在密码字段中发送密码密钥,我尝试单击该字段,清除该字段并发送密钥。但是现在使用任何一种方法。但是如果我调试和测试它可以工作

public class TestMail {
protected static WebDriver driver;
protected static String result;
@BeforeClass
public static void setup()  {
System.setProperty("webdriver.gecko.driver","D:\geckodriver.exe");
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
}
@Test
void Testcase1() {
driver.get("http://mail.google.com");
WebElement loginfield = driver.findElement(By.name("Email"));
if(loginfield.isDisplayed()){
loginfield.sendKeys("ragesh@gmail.in");
}
else{
WebElement newloginfield = driver.findElemnt(By.cssSelector("#identifierId"));                                      
newloginfield.sendKeys("ragesh@gmail.in");
// System.out.println("This is new login");
}

driver.findElement(By.name("signIn")).click();
// driver.findElement(By.cssSelector(".RveJvd")).click();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
// WebElement pwd = driver.findElement(By.name("Passwd"));
WebElement pwd = driver.findElement(By.cssSelector("#Passwd"));
pwd.click();
pwd.clear();
// pwd.sendKeys("123");
if(pwd.isEnabled()){
pwd.sendKeys("123");
}
else{
System.out.println("Not Enabled");
}

尝试将隐式等待时间设置为 10 秒。

gmail.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

或者设置显式等待。显式等待是您定义的代码,用于等待特定条件发生,然后再继续执行代码。在您的情况下,它是密码输入字段的可见性。(感谢ainlolcat的评论)

WebDriver gmail= new ChromeDriver();
gmail.get("https://www.gmail.co.in"); 
gmail.findElement(By.id("Email")).sendKeys("abcd");
gmail.findElement(By.id("next")).click();
WebDriverWait wait = new WebDriverWait(gmail, 10);
WebElement element = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.id("Passwd")));
gmail.findElement(By.id("Passwd")).sendKeys("xyz");

说明:Selenium 找不到该元素的原因是密码输入字段的 id 最初是 Passwd 隐藏的。单击"下一步"按钮后,Google首先验证输入的电子邮件地址,然后显示密码输入字段(通过将ID从Passwd隐藏更改为Passwd)。因此,当密码字段仍然隐藏时(即Google仍在验证电子邮件ID),您的网络驱动程序将开始搜索ID Passwd的密码输入字段,该字段仍处于隐藏状态。因此,将引发异常。

">

元素不可交互">错误可能意味着两件事:

a.元素未正确呈现:

解决方案只是使用隐式/显式等待

  • 隐式等待:

    driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);

  • 显式等待:

    WebDriverWait wait=new WebDriverWait(driver, 20); element1 = wait.until(ExpectConditions.elementToBeClickable(By.className("fa-stack-1x")));

b.元素已呈现,但不在屏幕的可见部分:

解决方案只是滚动到元素。根据Selenium的版本,它可以以不同的方式处理,但我将提供一个适用于所有版本的解决方案:

JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].scrollIntoView(true);", element1);
  1. 假设所有这些都失败了,那么另一种方法是再次使用Javascript执行器,如下所示:

    executor.executeScript("arguments[0].click();",元素 1);

  2. 如果您仍然无法单击 ,那么它可能再次意味着两件事:

1.内嵌框架

检查 DOM 以查看您正在检查的元素是否存在于任何帧中。如果为真,则需要在尝试任何操作之前切换到此帧。

driver.switchTo().frame("a077aa5e"); //switching the frame by ID
System.out.println("********We are switching to the iframe*******");
driver.findElement(By.xpath("html/body/a/img")).click();

2. 新标签

如果打开了一个新选项卡并且该元素存在于其上,那么您再次需要编写如下代码以在尝试操作之前切换到它。

String parent = driver.getWindowHandle();
driver.findElement(By.partialLinkText("Continue")).click();
Set<String> s = driver.getWindowHandles();
// Now iterate using Iterator
Iterator<String> I1 = s.iterator();
while (I1.hasNext()) {
String child_window = I1.next();
if (!parent.equals(child_window)) {
driver.switchTo().window(child_window);
element1.click() 
}

您也可以尝试完整的 xpath,我遇到了类似的问题,我必须单击具有属性 javascript onclick 函数的元素。 完整的 xpath 方法有效,并且没有引发可交互的异常。

请尝试像这样选择密码字段。

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement passwordElement = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#Passwd")));
passwordElement.click();
passwordElement.clear();
passwordElement.sendKeys("123");

在我的例子中,生成异常的元素是属于表单的按钮。我替换了

WebElement btnLogin = driver.findElement(By.cssSelector("button"));
btnLogin.click();

btnLogin.submit();

我的环境是 chromedriver windows 10

就我而言,我使用的是python-selenium。 我有两个指示。第二条指令无法执行。 我在两条指令之间放了一个time.sleep(1),我就完成了。

如果需要,您可以根据需要更改睡眠量。

就我而言,查询不够具体。页面上有多个具有相同属性的元素,因此找到的第一个元素不是我需要的元素。结果,"元素不可交互"错误是正确的,因为我试图单击错误的元素。我最终找到了所有"相同"元素的 id,并抓住了第 n 个而不是第一个。这可以解释为什么有些人说使用xpath对他们有用。

注意:我正在使用Clojure和Etaoin库,它是Selenium的包装器,因此术语可能不匹配

请参阅下面的简化示例。

抛出"元素不可交互"错误的错误代码:

(let [submit-button-query {:tag :button :fn/has-class "submit-yes"}
submit-button-element (query driver submit-button-query)] 
(click-el driver submit-button-element))  

工作代码:

(let [submit-button-query {:tag :button :fn/has-class "submit-yes"}   ;; query that I assumed was unique and matched the button I was interested in but, in fact, there were multiple elements with that class. I didnt realize it because I was using query fn that only returns the first match. Below Im using query-all fn to find all matches first
all-submit-button-elements (query-all driver submit-button-query)
pop-up-submit-button-element (last all-submit-button-elements)] ;; my case was simple and I could safely assume that the button I need is the last one from the list of matches 
(click-el driver pop-up-submit-button-element))                    ;; successful click 

我遇到了同样的问题,然后找出了原因。我试图输入一个范围标签而不是输入标签。我的 XPath 是用 span 标签编写的,这是一件错误的事情。我查看了该元素的 Html 并发现了问题。然后我所做的只是找到恰好是子元素的输入标签。仅当 XPath 是使用输入标记名称创建的时,才能键入输入字段

我要用这个来对冲这个答案:我知道这是废话......而且一定有更好的方法。(见以上答案)但是我在这里尝试了所有建议,但仍然没有得到。最终追逐错误,将代码撕成位。然后我试了这个:

import keyboard    
keyboard.press_and_release('tab')
keyboard.press_and_release('tab')
keyboard.press_and_release('tab') #repeat as needed
keyboard.press_and_release('space') 

这是非常难以忍受的,你必须确保你不会失去焦点,否则你只会在错误的事情上跳动和间隔。

我对为什么其他方法对我不起作用的假设是,我正在尝试单击开发人员不希望机器人单击的内容。所以我没有点击它!

我收到此错误,因为我在Selenium WebDriver Node中使用了错误的CSS选择器.js函数By.css()

您可以通过在 Web浏览器的 Web 控制台(Ctrl+Shift+K 快捷方式)中使用 JavaScript 函数document.querySelectorAll()来检查您的选择器是否正确。

如果它在调试中工作,那么wait一定是正确的解决方案.
我建议使用显式wait,如下所示:

WebDriverWait wait = new WebDriverWait(new ChromeDriver(), 5);
wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#Passwd")));

我也遇到了这个错误。 我想这可能是因为该字段不可见。 我尝试了上面的滚动解决方案,尽管该字段在受控浏览器会话中变得可见,但我仍然收到异常。 我提交的解决方案类似于下面。 看起来事件可以冒泡到包含的输入字段,最终结果是Selected属性变为 true。

该字段在我的页面中出现如下内容。

<label>
<input name="generic" type="checkbox" ... >
<label>

通用工作代码看起来或多或少如下所示:

var checkbox = driver.FindElement(By.Name("generic"), mustBeVisible: false);
checkbox.Selected.Should().BeFalse();
var label = checkbox.FindElement(By.XPath(".."));
label.Click();
checkbox.Selected.Should().BeTrue();

您需要将其翻译成您的特定语言。 我正在使用C#和FluentAssertions。 这个解决方案适用于Chrome 94和Selenium 3.141.0。

我必须先将鼠标悬停在元素上才能显示子元素。起初我没有考虑到这一点。

WebElement boardMenu = this.driver.findElement(By.linkText(boardTitle));
Actions action = new Actions(this.driver);
action.moveToElement(boardMenu).perform();

另一个提示是检查您是否具有该 DOM 的一个元素。 尝试在检查网页时使用 Ctrl+F 并在那里检查您的 xpath;如果您使用 findElement 方法,它应该返回一个元素。

最新更新