Java 如何单击<;textarea>;根据下面的HTML?

Java 如何单击<;textarea>;根据下面的HTML?,java,selenium,selenium-webdriver,xpath,css-selectors,Java,Selenium,Selenium Webdriver,Xpath,Css Selectors,我正在做一个巨大的自动化项目,这是我完成这个项目之前的最后一期。我正在使用Selenium来自动化Drupal风格网站上的更改。到目前为止,我已经能够点击并与网站上的每个web元素进行交互(特别是当我停止使用chrome驱动程序时,无论出于何种原因,它都无法点击包装在标签中的元素?)。无论如何,我最近发现(在使用FirefoxGeckodriver时,仅供参考)我无法点击。执行此操作时,我得到以下错误: Element <textarea id="edit-body-und-0-value

我正在做一个巨大的自动化项目,这是我完成这个项目之前的最后一期。我正在使用Selenium来自动化Drupal风格网站上的更改。到目前为止,我已经能够点击并与网站上的每个web元素进行交互(特别是当我停止使用chrome驱动程序时,无论出于何种原因,它都无法点击包装在
标签中的元素?)。无论如何,我最近发现(在使用FirefoxGeckodriver时,仅供参考)我无法点击。执行此操作时,我得到以下错误:

Element <textarea id="edit-body-und-0-value" class="text-full wysiwyg form-textarea wysiwyg-processed" name="body[und][0][value]"> could not be scrolled into view
HTML

失败原因:线程“AWT-EventQueue-0”org.openqa.selenium.WebDriverException:TypeError:rect未定义异常

使用Javascript执行器不会将滚动保存到元素。单击仅单击屏幕右上角

使用
WebDriverWait
对象超时,因为该元素永远无法在时间上单击

我不知道是什么让点击这个元素如此困难,但我想我今天发现了一个线索。我不是JQuery(或一般的JS)工作原理方面的专家,但此脚本位于页面顶部:

jQuery.extend(Drupal.settings, {"basePath":....
在相当大的脚本中,作为元素的唯一元素ID被附加到名为“触发器”的对象。以下是脚本的一部分:

 "triggers": {
            "edit-field-office-address-und-0-value": {
                "field": "edit-field-office-address-und-0-value",
                "resizable": 1,
                "select": "edit-field-office-address-und-0-format--2",
                "formatfiltered_html": {
                    "editor": "ckeditor",
                    "status": 1,
                    "toggle": 1
                }
            },
            "edit-body-und-0-value": {
                "field": "edit-body-und-0-value",
                "resizable": 1,
                "select": "edit-body-und-0-format--2",
                "formatfiltered_html": {
                    "editor": "ckeditor",
                    "status": 1,
                    "toggle": 1
                }
            },
            "edit-field-requirements-und-0-value": {
                "field": "edit-field-requirements-und-0-value",
                "resizable": 1,
                "select": "edit-field-requirements-und-0-format--2",
                "formatfiltered_html": {
                    "editor": "ckeditor",
                    "status": 1,
                    "toggle": 1
                }
            }
        }
    },
我认为Javascript不知怎么搞砸了点击选项。也许它会创建格式选项,从而使元素看起来不可访问

我不知道该怎么办,最后期限快到了!如果您能帮助我了解如何点击并与此文本区域交互,我将不胜感激

您可以使用下面的“尝试”

WebDriverWait wait = new WebDriverWait(driver, 5)
JavascriptExecutor js = ((JavascriptExecutor) driver)

//Present in DOM
wait.until(ExpectedConditions.presenceOfElement(By.id("ID")));

//scroll to view
WebElement element = driver.findElement(By.id("ID")));  
js.executeScript("arguments[0].scrollIntoView(true);", element);

//Click
wait.until(ExpectedConditions.elementToBeClickable(By.id("ID")));

我以前也遇到过类似的问题,当元素不在屏幕上时,即使它们出现在网页上,驱动程序也不会捕获它们(您必须向上或向下滚动才能处理它们)。在检查我的项目时,我发现这个问题是由一个过时的chrome驱动程序引起的

以下是我建议的解决方案:

  • 尝试调试,在找到textarea元素之前设置断点。当调试执行在断点处暂停时,滚动页面,使文本区域可见。继续执行。如果问题仍然存在,您可能必须检查选择器。但是,如果手动滚动干预没有遇到问题,则可能是驱动程序/浏览器问题

  • 确保您的Chrome驱动程序已更新,并且机器上已安装Chrome浏览器。Chrome驱动程序的最新版本通常发布在此处:

  • 如果更新驱动程序/浏览器不起作用或不适用,作为一种解决方法,您可以尝试在使用actions builder获取元素之前插入“向上翻页”或“向下翻页”操作

  • 下面是一个示例代码:

    Actions actions = new Actions(driver);
        actions
            .moveToElement(anyVisibleElement)
            .sendKeys(Keys.PAGE_UP)
            .build()
            .perform();
    

    要单击默认文本为的文本区域,具体项目取决于学生的兴趣、背景和完成的课程。您必须诱导WebDriverWait,使所需元素可单击,如下所示:

    • css选择器

      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("textarea.text-full.wysiwyg.form-textarea#edit-body-und-0-value>p"))).click();
      
    • xpath

      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//textarea[@class='text-full wysiwyg form-textarea' and @id='edit-body-und-0-value']/p"))).click();
      
    此操作失败,出现“等待元素可点击”错误:/
    Actions actions = new Actions(driver);
        actions
            .moveToElement(anyVisibleElement)
            .sendKeys(Keys.PAGE_UP)
            .build()
            .perform();
    
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("textarea.text-full.wysiwyg.form-textarea#edit-body-und-0-value>p"))).click();
    
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//textarea[@class='text-full wysiwyg form-textarea' and @id='edit-body-und-0-value']/p"))).click();