我正在使用selenium-python绑定来执行一些基本的浏览器测试。
我需要单击一个默认不可见的元素。我已经通过is_displayed() method
进行了检查。因此,我尝试使用execute_script
方法通过使用以下代码使其可见,但我收到错误消息。这是一个常见的场景,我们需要让一个看不见的元素可见。必须有其他一些方法来绕过这类问题。如果有人在我的代码中指出了问题,那将会很有帮助。我可以在python代码中单击带有纯js的元素吗?
print "getting keyword ideas"
searches = driver.find_element_by_xpath("//*[contains(text(), 'Avg. monthly searches')]")
driver.execute_script("arguments.style.visibility='visible';", searches)
和错误消息:
追踪(最近一次通话): 文件" C:\ vhosts \ phpsols \ splinter \ adwords.py",第140行,在 driver.execute_script(" arguments.style.visibility =' visible';",搜索) 文件" C:\ anaconda32 \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py", 第429行,在execute_script中 {'脚本&#39 ;:脚本,' args':converted_args})['价值'] 文件" C:\ anaconda32 \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py", 201行,执行中 self.error_handler.check_response(响应) 文件" C:\ anaconda32 \ lib \ site-packages \ selenium \ webdriver \ remote \ errorhandler.py", 第181行,在check_response中 提出exception_class(消息,屏幕,堆栈跟踪) selenium.common.exceptions.WebDriverException:消息:{" errorMessage":" undefined不是对象(评估 ' arguments.style.visibility ='可见'')""请求" {"头" {&# 34;接受":"应用/ JSON""接受编码":"身份""连接":&#34 ;靠近"" Content-Length的":" 210""内容类型":"应用/ JSON;字符集= UTF-8& #34;"主机":" 127.0.0.1:55867""用户代理":" Python的的urllib / 2.7" }," httpVersion":" 1.1""方法":" POST""后":&# 34; {\"的sessionId \&#34 ;: \" 8f05e120-f672-11e5-91c5-c7097c43ddb4 \",\" args \": [{\" element-6066-11e4-a52e-4f735466cecf \":\":wdc:1459340795815 \", \" ELEMENT \":\":wdc:1459340795815 \"}],\" script \": \" arguments.style.visibility ='可见&#39 ;; \"}"" URL":" /执行&#34 ;, " urlParsed" {"锚":"""查询":"""文件":"执行""目录":" /""路径":" /执行&#34 ;,"相对于":" /执行""端口":"""主机":&# 34;""密码":"""使用者":""" USERINFO&#34 ;:"""权威":"""协议":""&#34 ;源":" /执行"" queryKey":{},"大块":["执行"]},& #34; urlOriginal":" /会话/ 8f05e120-f672-11e5-91c5-c7097c43ddb4 /执行"}} 屏幕截图:可通过屏幕获取
<div id="gwt-debug-column-SEARCH_VOLUME_PRIMARY-header-0-0">
<div style="text-align:right">Avg. monthly searches
<span class="table-tooltip" id="gwt-debug-tooltip-4580983"> </span>
<br>
</div>
</div>
这是python代码:
print "getting keyword ideas"
driver.execute_script('document.getElementById("gwt-debug-column-SEARCH_VOLUME_PRIMARY-header-0-0").style.visibility = "visible";')
driver.execute_script('document.getElementById("gwt-debug-column-SEARCH_VOLUME_PRIMARY-header-0-0").style.display = "block";')
searches = driver.find_element_by_xpath("//*[contains(text(), 'Avg. monthly searches')]")
if searches.is_displayed():
print "searches is visible"
else:
print "searches isn't visible"
avg_monthly_searches = driver.find_elements_by_id("gwt-debug-column-SEARCH_VOLUME_PRIMARY-header-0-0")
for avg in avg_monthly_searches:
if avg.is_displayed():
print "element is visible, so clicking ..."
actions = ActionChains(driver)
actions.click(on_element=avg).perform()
time.sleep(10)
print "scrolling to the bottom"
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
print "getting the page source as interpreted by chromedriver"
driver.execute_script('return document.documentElement.innerHTML')
print "getting keyword ideas source"
content = driver.page_source
with open('keyword_ideas.html', 'w') as f:
f.write(content.encode('utf-8'))
time.sleep(5)
print "getting html"
dom = DOM(content)
print "traversing ... "
for e in dom('td.spgb-f'):
for a in e('a.sptc-e.sptc-h'):
print repr(plaintext(a.content))
else:
print "element isn't visible"
答案 0 :(得分:0)
请尝试以下代码:
driver.execute_script('document.getElementById("gwt-debug-column-SEARCH_VOLUME_PRIMARY-header-0-0").style.visibility = "visible";')
driver.execute_script('document.getElementById("gwt-debug-column-SEARCH_VOLUME_PRIMARY-header-0-0").style.display = "block";')