2017-04-01 93 views
0

鏈接http://www.babylegs.comPython Selenium如何在元素上移動鼠標。這表明下拉菜單

我的代碼:

class TestClassMy(unittest.TestCase): 
    def setUp(self): 
     self.driver = webdriver.Firefox() 


    def test1(self): 
     driver = self.driver 
     driver.get('http://www.babylegs.com') 
     driver.maximize_window() 
     element_to_select = driver.find_element_by_xpath(".//*[@id='nav']/ol/li[5]/a") #d.send_keys(Keys.NULL) 

     actions = ActionChains(driver) 
     element_to_select.click_and_hold(element_to_select).perform() 

    def tearDown(self): 
     self.driver.close() 

if __name__ == '__main__': 
    unittest.main() 

回答

0

使用ActionChains這樣的:

actions.move_to_element(element_to_select).perform() 

然後,你可以做你的需要。這就是你將如何使用下拉菜單,通過finding元素,然後執行一個鏈,從根本上將鼠標移動到正確的位置。

然後,一旦菜單(和子菜單)已經暴露,您可以點擊它們(正如你看到的,你不能點擊的東西不可見。

在你的情況下,選擇從「襪子的東西「菜單:

e1 = driver.find_element_by_xpath('//*[@id="nav"]/ol/li[5]/a') 
e2 = e1.find_element_by_xpath('../ul/li[1]/a') 
actions.move_to_element(e1).move_to_element(e2).perform() 
e2.click() 
+0

兄弟,就你 我最後的代碼如下所示: https://gyazo.com/481d67e4acaf6dad642d462cc4ff0fd3 出於某種原因,它不工作於Firefox(((( 當我將瀏覽器更改爲Chrome瀏覽器時,它可以幫助我Firefox? – ChantOfSpirit

+0

如果您收到錯誤消息「moveto與已知命令不匹配」,則表明Firefox對操作的支持受到了限制。例如https://github.com/SeleniumHQ/selenium/issues/2285。該錯誤報告表明15天前修復,所以也許如果你升級到最新的Selenium(3.3+)和最新的geckodriver(0.15+),它將工作。 – pbuck

+0

謝謝! ☺☺☺☺☺ – ChantOfSpirit

相關問題