Python Selenium Webdriver:单击下拉列表中的按钮时出现问题<;李>;
我在使用SeleniumWebDriver单击按钮时遇到问题。我尝试过使用等待,但没有任何效果,因为我是新手,所以我可能没有正确地使用等待。我还收到了一个关于全局驱动程序问题的错误消息,但再次强调,我做得不对,也没有找到正确的方法来解决这个问题 我得到的错误是:Python Selenium Webdriver:单击下拉列表中的按钮时出现问题<;李>;,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,我在使用SeleniumWebDriver单击按钮时遇到问题。我尝试过使用等待,但没有任何效果,因为我是新手,所以我可能没有正确地使用等待。我还收到了一个关于全局驱动程序问题的错误消息,但再次强调,我做得不对,也没有找到正确的方法来解决这个问题 我得到的错误是: ERROR: test_categorySelect (__main__.TitleTest) ----------------------------------------------------------------------
ERROR: test_categorySelect (__main__.TitleTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "auto2.py", line 24, in test_categorySelect
category = self.wait.until(EC.visibility_of_element_located((By.XPATH, "//li[]@id='menu-item-34']/a")))
File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/support/wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
TimeoutException: Message:
Screenshot: available via screen
----------------------------------------------------------------------
Ran 2 tests in 15.817s
以下是演示站点:
我尝试单击的区域的代码如下:
<ul class="sub-menu">
<li id="menu-item-34" class="menu-item menu-item-type-taxonomy menu-item-object-wpsc_product_category menu-item-34">
<span class="before"> </span>
<a href="http://store.demoqa.com/products-page/product-category/accessories/">
<span></span>
Accessories
</a>
</li>
问题是它在等待子菜单可见时超时,但在调用wait.until之前没有执行任何操作 您需要使用ActionChains将鼠标悬停在“产品类别”上,然后单击特定类别
def test_categorySelect(self):
self.driver.get("http://store.demoqa.com")
product_category = self.driver.find_element_by_css_selector("li[id='menu-item-33']")
category = self.driver.find_element_by_css_selector("li[id='menu-item-34']")
ActionChains(self.driver).move_to_element(product_category).click(category).perform()
您有一个额外的结束括号],请将您的定位符更改为:
“//li[@id='menu-item-34']/a”
尝试使用self.driver,而不仅仅是driver,以避免全局驱动程序问题eam我错过了您所写的等待?我添加了您给我的代码,但仍然收到“element not visible”消息。我正在试图找出在何处添加等待。:(我使用self.driver=webdriver.Chrome()测试了上述函数,它在没有等待的情况下工作。如果您想在中间添加等待,可以尝试按此处所述的步骤执行ActionChain。您可以在调用将_移动到_元素()和单击()之间放置等待)你说的100%对!我可能会放弃运行幻影进行测试,这似乎是不必要的。Chrome工作得很好,谢谢你,我会查看你在使用waits时发布的链接。很高兴听到它工作了,如果它解决了你的问题,你能接受答案吗?谢谢Pratek,我调整了。
def test_categorySelect(self):
self.driver.get("http://store.demoqa.com")
product_category = self.driver.find_element_by_css_selector("li[id='menu-item-33']")
category = self.driver.find_element_by_css_selector("li[id='menu-item-34']")
ActionChains(self.driver).move_to_element(product_category).click(category).perform()