Python中,
actions = ActionChains(driver)
如果driver进行了重新加载,也就是driver进行了重定向或者driver的网址进行了更改,selenium的行为链就需要重新载入,否则它处理的还是原先的网页,而原先的网页已经不在了,程序就会报错
from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.firefox.service import Service from selenium.webdriver.common.by import By driver_path = '/home/jerry/firefox/geckodriver' s = Service(driver_path) driver = webdriver.Firefox(service=s) driver.get('https://lcj.ink') actions = ActionChains(driver) a = driver.find_element(By.XPATH,'//li[@id="menu-item-45"]//a[@href="https://lcj.ink/wp-login.php"]') actions.click(a) driver.get('https://lcj.ink/wp-login.php') actions = ActionChains(driver) #...... actions.perform()