点击样式=“显示:块;";在Selenium WebDriver中 [英] clicking on an style="display: block;" in selenium webdriver

查看:109
本文介绍了点击样式=“显示:块;";在Selenium WebDriver中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图单击样式为 style ="display:block;" 的元素,但无法这样做.

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

下面是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("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();

错误日志:

Cannot read property 'style' of null.

推荐答案

您正在尝试将Javascript应用于具有id="qEgULq"的元素,而需要具有class="qEgULq"的元素:

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

只需替换

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

使用

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

还请注意,您提供的代码中的click();并未绑定到元素

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

更新

如果某些类名是动态生成的或仅在某些事件上分配的,则可以避免将类名用作定位符:

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);

这篇关于点击样式=“显示:块;";在Selenium WebDriver中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆