无法在selenium-python中通过xpath选择按钮


button = self.driver.find_element_by_xpath(
"/html/body/div[4]/div[2]/div/div[1]/main/div/div[2]/div/div/div/form/div[3]/button")
button.click()

当我尝试运行我的代码时,我得到了异常:selenium.com.mon.exceptions.NoSuchElementException:消息:没有这样的元素:找不到元素:{quot;方法":quot;xpath"选择器"会话信息:chrome=91.0.4472.164

同时是找到的按钮的xpathhttps://www.zalando.fr/login

这就是元素

Se连接器

我试着通过类名和所有的东西来找到它,但不起作用

您的定位器错误
还使用显式等待
试试这个:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button[data-testid='login_button']"))).click()

您可以在需要处理cookie窗口的地方尝试此操作,该窗口在页面加载并使用explicitWait后显示

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
driver.get("https://www.zalando.fr/login")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[normalize-space()='OK']"))).click()
#here I'm passing the user name
driver.find_element_by_xpath("//*[@id='login.email']").send_keys("email")
#here passing password
driver.find_element_by_xpath("//*[@id='login.password']").send_keys("password")
#clicking on the `Se connecter` button
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[@data-testid='login_button']"))).click()

xpath最终出错。我从chrometools复制了它,后来用brave进行了尝试,得到了正确的xpath。

最新更新