JavaScriptExecutor + WebDrive com erro:无法读取null的属性“ click”

时间:2019-10-20 02:38:58

标签: javascript selenium-webdriver

编辑为英语

我正在浏览**我没有编辑权限**的页面,需要单击引用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()”)时,硒在当前窗口中打开了另一个指南,因此它丢失了上下文,但是我没有知道如何解决。

1 个答案:

答案 0 :(得分:0)

长时间工作后,我发现了问题:

导航的上一步,我单击了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();"); 
    }
}