[英]WebDriver. Python. I want to store one of the option of drop-down menu by value as text, how to do it?
[英]Python Selenium WebDriver. How to check/verify that drop-down menu with suggested results is displayed?
我想确保在我向搜索字段中输入内容时显示带有建议结果的下拉菜单。
这是我的脚本(不适用于Google搜索),该脚本不起作用:
suggestions = driver.find_element_by_xpath('/html/body/div[6]')
print suggestions.get_attribute('display') # >>>None
if suggestions == "block":
print "Suggestions are displayed!"
else:
print "Suggestions aren't displayed!"
据我了解,我必须检查“ display”属性是否为“ block”。 如果为“ none”,则表示不显示下拉菜单。
带有建议结果的菜单的HTML代码:
<div style="display: block; position: absolute; width: 237px; top: 270px; left: 186px;" class="ac_results">
<ul style="max-height: 180px; overflow: auto;">
<li class="ac_even ac_over">Elen<strong>a</strong> J<strong>a</strong>mes De<strong>a</strong>n</li>
<li class="ac_odd">Ellie portnov T<strong>a</strong>r<strong>a</strong></li>
<li class="ac_even">Elen<strong>a</strong> Q<strong>A</strong></li>
<li class="ac_odd">Jessy J<strong>a</strong>mes</li>
<li class="ac_even">J<strong>a</strong>mes HotStuff De<strong>a</strong>n</li>
<li class="ac_odd">j<strong>a</strong>mess b<strong>a</strong>g de<strong>a</strong>n</li>
<li class="ac_even">J<strong>a</strong>mes Hotstuff De<strong>a</strong>n</li>
<li class="ac_odd">j<strong>a</strong>mes cool De<strong>a</strong>n</li>
<li class="ac_even">J<strong>a</strong>smin1 Gurusw<strong>a</strong>mi1</li>
<li class="ac_odd">j<strong>a</strong>mes hotguy de<strong>a</strong>n</li>
</ul>
</div>
我的代码:
suggestions = driver.find_element_by_xpath('/html/body/div[6]')
if suggestions.is_displayed():
print "Suggestions are displayed!"
else:
print "Suggestions aren't displayed!"
style
is_displayed()
WebElement的方法。 如果显示元素,则返回True
。 您的代码是完全错误的。
suggestions = driver.find_element_by_xpath('/html/body/div[6]') style = suggestions.get_attribute('style') if 'block' in style: print "Suggestions are displayed!" else: print "Suggestions aren't displayed!"
这可以帮助,但更好地使用数字3;)
更新:由于需要花费一些时间来呈现所需的元素,因此使用等待是一个好习惯。 信息在这里
UPDATE2:您可以使用以下代码:
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
WebDriverWait(driver, 5).until(
EC.visibility_of_element_located((By.XPATH, '/html/body/div[6]')),
"Element was not displayed after 5 seconds")
如果元素的可见性不重要,则可以使用try-except块
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.