gpt4 book ai didi

Python Selenium 如何在元素上移动鼠标。显示下拉菜单

转载 作者:行者123 更新时间:2023-12-04 00:35:45 24 4
gpt4 key购买 nike

链接 http://www.babylegs.com

我的代码:

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()

最佳答案

假设您的浏览器是 Firefox,Python 代码如下所示:

driver = webdriver.Firefox(executable_path=driver_path)
action = webdriver.ActionChains(driver)
element = driver.find_element_by_id('your-id') # or your another selector here
action.move_to_element(element)
action.perform()

如果您已经将光标移动到一个元素并希望相对地重新定位它,您可以使用:
action.move_by_offset(10, 20)    # 10px to the right, 20px to bottom
action.perform()

甚至更短:
action.move_by_offset(10, 20).perform()

更多文档在这里:
https://selenium-python.readthedocs.io/api.html

关于Python Selenium 如何在元素上移动鼠标。显示下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43161987/

24 4 0