【问题标题】:JavaScriptExecutor + WebDrive com erro: Cannot read property 'click' of nullJavaScriptExecutor + WebDriver com 错误:无法读取 null 的属性“单击”
【发布时间】:2019-10-20 02:38:58
【问题描述】:

已编辑为英文

我正在浏览的页面**我没有编辑权限**并且需要单击引用 JavaScript 函数的文本。

  • 使用 Selenium WebDriver 进行导航;
  • 使用 ChromeDriver;
  • 使用JavaScriptExecutor点击html标签
<div id="btn-certificacao-digital" style="display: block;">
     <p>AUTENTICAÇÃO COM</p>
     <br>
     <strong>CERTIFICADO DIGITAL</strong>
     <a href="javascript:ICPShow();" id="btnCallLogin" class="access-details"> 
     </a>
</div>

我正在尝试这个:

public class ArispCrawler {
    public void mainArispFlow() {

        WebDriver driver = MainWebDriver.AutenticaMock();
        driver.findElement(By.linkText("Arisp")).click();
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("document.getElementById('btnCallLogin').click()");
    }
}

我的 WebDrive 的配置:

    public static WebDriver ConfigNavDrive() {
        System.setProperty("webdriver.chrome.driver", "C://Program Files//Selenium//chromedriver.exe");
        WebDriver driver = new ChromeDriver(); 
        driver.manage().window().maximize();
        return (driver);
    }

错误日志:

Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
[1571537382.956][WARNING]: Timed out connecting to Chrome, retrying...
2019-10-19 23:09:44.983  INFO 15576 --- [ null to remote] o.o.selenium.remote.ProtocolHandshake    : Detected dialect: W3C
[1571537387.001][WARNING]: Timed out connecting to Chrome, retrying...
Exception in thread "main" org.openqa.selenium.JavascriptException: javascript error: Cannot read property 'click' of null
  (Session info: chrome=77.0.3865.120)
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:48'
System info: host: 'NOTETI-20', ip: '192.168.0.106', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_211'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 77.0.3865.120, chrome: {chromedriverVersion: 77.0.3865.40 (f484704e052e0..., userDataDir: C:\Users\tleite\AppData\Loc...}, goog:chromeOptions: {debuggerAddress: localhost:55279}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 5a7294d553560d624269a181a6406b00

OBS:其他简单的导航都可以正常工作,只是使用 JavaScriptExecutor 会出现问题。也通过单击 div 进行了测试,但它不起作用

更新

问题可能是当我使用 js.executeScript("document.getElementById('btnCallLogin').click()") 时,selenium 在当前窗口中打开另一个指南,因此它丢失了上下文,但我没有知道如何解决它。

【问题讨论】:

    标签: javascript selenium-webdriver


    【解决方案1】:

    经过长时间的工作,我发现了问题:

    在导航中的上一步,我单击了在导航器中打开新标签的 href:

    <a href="arisp/login.html" target="_blank">Arisp</a>
    

    我用它在同一个标​​签中打开:

    public class ArispCrawler {
        public void mainArispFlow() throws InterruptedException {
    
            WebDriver driver = MainWebDriver.AutenticaMock();
            String urlArisp = driver.findElement(By.linkText("Arisp"))
                    .getAttribute("href");
            driver.get(urlArisp);
            Thread.sleep(5000);
            JavascriptExecutor js = (JavascriptExecutor) driver;
            js.executeScript("document.getElementById('btnCallLogin').click()");
            //String urlAutenticacao = driver.findElement(By.id("btnCallLogin"))
            //      .getAttribute("javascript:ICPShow();"); 
        }
    }
    
    
    

    【讨论】:

      猜你喜欢
      • 2014-07-25
      • 2021-11-07
      • 2016-05-08
      • 2017-06-07
      • 1970-01-01
      • 1970-01-01
      • 2022-07-27
      • 1970-01-01
      • 2021-12-03
      相关资源
      最近更新 更多