我正在尝试点击一个元素,其格式为 style =" display:block;" 但无法执行此操作。
下面是html
代码
<div class="fl f18 dtoggler pointer underline some-padding new_data_entry" data-div-id="eBWJcg" data-div-ref="new_Passport_form.eBWJcg">+ Add Passport</div>
<div class="clear new_Passport_form qEgULQ some-margin togglable" style="display: none;">
Selenium
代码:
driver.get("idfystaging.com/users/sign_in");
driver.findElement(By.name("user[login]")).sendKeys("rustam1@yopmail.com");
driver.findElement(By.name("user[password]")).sendKeys("Password123");
driver.findElement(By.name("commit")).click();
driver.switchTo().frame("upload_iframe_1");
driver.findElement(By.cssSelector("body")).sendKeys(Keys.ESCAPE);
JavascriptExecutor executor= (JavascriptExecutor)driver;
executor.executeScript("document.getElementById('qEgULQ').style.display='block';");click();
错误日志:
Cannot read property 'style' of null.
答案 0 :(得分:0)
qEgULQ
是类,而不是id。尝试
executor.executeScript("document.getElementsByClassName('qEgULQ').style.display='block';");
答案 1 :(得分:0)
当您需要Javascript
元素时,您尝试将id="qEgULq"
应用于包含class="qEgULq"
的元素:
只需替换
executor.executeScript("document.getElementById('qEgULQ').style.display='block';");
与
executor.executeScript("document.querySelector('div.clear.new_Passport_form.qEgULQ.some-margin.togglable').style.display='block';");
另请注意,您提供的代码段中的click();
并未绑定到元素
<强>更新强>
如果某些类名是动态生成的,或仅在某些事件上分配,则可以避免使用类名作为定位器:
WebElement targetDiv = driver.findElement(By.xpath("//div[text()='+ Add Passport']/following-sibling::div"));
executor.executeScript("arguments[0].style.display='block';", targetDiv);