JavascriptExecutor 在 Selenium 中无法正常工作。

huangapple go评论96阅读模式
英文:

JavascriptExecutor doesn't work properly with Selenium

问题

以下是翻译好的部分:

 private void inputWorkDescription(WebDriver driver, int rawNumber) throws IOException, GeneralSecurityException {
        if (!getWorkDescriptionFromSheets(rawNumber).isEmpty()) {
            WebDriverWait wait = new WebDriverWait(driver, WAITING_PERIOD);
            WebElement workDescription = wait.until(ExpectedConditions.visibilityOfElementLocated(
                    By.cssSelector("span.placeholder-decoration.ProseMirror-widget span")));
            JavascriptExecutor executor = (JavascriptExecutor) driver;
            executor.executeScript("arguments[0].click();", workDescription);          // ---WORKS
            executor.executeScript("arguments[0].value='123456';", workDescription);   // --- DOESN'T WORK
            executor.executeScript("arguments[0].setAttribute('value', '" 
+ getWorkDescriptionFromSheets(rawNumber) + "');", workDescription);   // --- DOESN'T WORK
        }
    }

很遗憾,它不能正常工作。我只看到click命令被执行,但没有文字输入。
我尝试了不同的文本输入选项,但都不起作用。

英文:

I use JavascriptExecutor to input the text. My code snippet is as follows

 private void inputWorkDescription(WebDriver driver, int rawNumber) throws IOException, GeneralSecurityException {
        if (!getWorkDescriptionFromSheets(rawNumber).isEmpty()) {
            WebDriverWait wait = new WebDriverWait(driver, WAITING_PERIOD);
            WebElement workDescription = wait.until(ExpectedConditions.visibilityOfElementLocated(
                    By.cssSelector("span.placeholder-decoration.ProseMirror-widget span")));
            JavascriptExecutor executor = (JavascriptExecutor) driver;
            executor.executeScript("arguments[0].click();", workDescription);          // ---WORKS
            executor.executeScript("arguments[0].value='123456';", workDescription);   // --- DOESN'T WORK
            executor.executeScript("arguments[0].setAttribute('value', '" 
+ getWorkDescriptionFromSheets(rawNumber) + "');", workDescription);   // --- DOESN'T WORK
        }
    }

Unfortunately, it doesn't work properly. I just see that click command performed but no text input.
I tried different options for text input but it doesn't work.

答案1

得分: 0

我使用了错误的 CSS 标识符。

对于我的问题,答案在下面的截图中:

JavascriptExecutor 在 Selenium 中无法正常工作。

英文:

I used wrong css identifier.

The answer on my question is on screenshot below

JavascriptExecutor 在 Selenium 中无法正常工作。

huangapple
  • 本文由 发表于 2020年10月16日 03:14:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/64378311.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定