我遇到使用Selenium Webdriver点击按钮的问题。我试过等待无济于事,但我可能没有正确地做到这一点,尽管我不熟悉这一点。我也收到有关全球驾驶员问题的错误,但同样,我没有做到这一点,并且无法找到正确的方法来解决这个问题。
错误我得到了:
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>
这是我目前的代码:
import unittest
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class TitleTest(unittest.TestCase):
def setUp(self):
# self.driver = webdriver.Chrome()
self.driver = webdriver.PhantomJS()
self.wait = WebDriverWait(driver, 10)
def test_title(self):
self.driver.get("http://store.demoqa.com")
self.assertIn("STORE", self.driver.title)
print "================================================================"
print "==== Successful Title Match ===="
print "================================================================"
def test_categorySelect(self):
category = self.wait.until(EC.visibility_of_element_located((By.XPATH, "//li[]@id='menu-item-34']/a")))
ActionChains(self.driver).move_to_element(category).click().perform()
print "================================================================"
print "==== Successful Accessories Button Clicked ===="
print "================================================================"
def tearDown(self):
self.driver.quit()
if __name__ == "__main__":
unittest.main()
答案 0 :(得分:0)
问题在于等待子菜单显示超时,但在调用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()
答案 1 :(得分:0)
您还有一个额外的右括号] ,请将您的定位器更改为:"//li[@id='menu-item-34']/a"