或许,如果你要使用全部xpath来确定元素,就是指bsxpath
。
abs-xpath as this /html/one/div[3]/div/div/div/div/div/form/input, which from the 网站
您应制定这样的守则:
driver.find_element(By.XPATH, r"/html/body/div[3]/div/div/div/div/div[2]/form/input")
WebDriverWait(driver, timeout=10).until(EC.element_to_be_clickable((By.XPATH, r"/html/body/div[3]/div/div/div/div/div[2]/form/div[2]/div[2]/button")))
driver.find_element(By.XPATH, "/html/body/div[3]/div/div/div/div/div[2]/form/div[2]/div[2]/button").click()
<>strong>besides, 您可使用另一种方法查找元素,如:
driver.find_elements(By.CLASS_NAME, "username-input")[0]
driver.find_elements(By.TAG_NAME, a )[0]
driver.find_elements(By.ID, userSkin )[0]
the last, maybe you should use EC.* of selenium to wait for something happen
you click and html reload, then some element can be located
like login or logout some website, you should wait until page reload and some element can be located.
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, timeout=10).until(EC.element_to_be_clickable((By.XPATH, r"")))
WebDriverWait(driver, timeout=10).until(EC.visibility_of_all_elements_located((By.XPATH, r"")))
WebDriverWait(driver, timeout=10).until(EC.presence_of_element_located((By.XPATH, r"")))
WebDriverWait(driver, timeout=10).until(EC.title_contains((By.XPATH, r"")))
WebDriverWait(driver, timeout=10).until(EC.text_to_be_present_in_element((By.XPATH, r"")))