clicking on an style=“display: block;” in selenium webdriver

只谈情不闲聊 提交于 2019-12-13 07:05:47

问题


i am trying to click on an element for which style is style="display: block;" but not able to to do so.

below is the html code

<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 code:

driver.get("idfystaging.com/users/sign_in"); 
driver.findElement(By.name("user[login]")).sendKeys("rustam1‌​@yopmail.com"); 
driver.findElement(By.name("user[password]")).sendKeys("Pass‌​word123"); 
driver.findElement(By.name("commit")).click();     
driver.switchTo().frame("upload_iframe_1");
driver.findElement(By.cssSelector("body")).sendKeys(Keys.ESC‌​APE); 
JavascriptExecutor executor= (JavascriptExecutor)driver;
executor.executeScript("document.getElementById('qEgULQ').st‌​yle.display='block';‌​");click();

Error log:

Cannot read property 'style' of null.

回答1:


qEgULQ is class, not id. Try

executor.executeScript("document.getElementsByClassName('qEgULQ').st‌​yle.display='block';‌​");



回答2:


You're trying to apply Javascript to element with id="qEgULq" while you need element with class="qEgULq":

Just replace

executor.executeScript("document.getElementById('qEgULQ').st‌​yle.display='block';‌​");

with

executor.executeScript("document.querySelector('div.clear.new_Passport_form.qEgULQ.some-margin.togglable').st‌​yle.display='block';‌​");

Also note that your click(); from provided piece of code doesn't bound to element

Update

If some of class names are generated dynamically or assigned just on some events, you can avoid using class names as locator:

WebElement targetDiv = driver.findElement(By.xpath("//div[text()='+ Add Passport']/following-sibling::div"));
executor.executeScript("arguments[0].st‌​yle.display='block';‌​", targetDiv);


来源:https://stackoverflow.com/questions/42526292/clicking-on-an-style-display-block-in-selenium-webdriver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!