首页 > 解决方案 > Python Selenium 没有这样的元素:无法找到 xpath

问题描述

我正在尝试自动登录到我是用户的投注网站,但我无法完成登录,因为 selenium 找不到 xpath。这是我到目前为止使用的代码:

from time import sleep
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import login_data

def main():
    options = webdriver.ChromeOptions() 
    options.headless = False
    options.add_experimental_option("excludeSwitches", ["enable-logging"])
    browser = webdriver.Chrome(options=options, executable_path=r'C:\Users\bilakos\Documents\DRIVERS\chromedriver.exe')
    browser.get("https://en.stoiximan.gr/")
    sleep(5)

    login_data.login(browser)


main()

这是导入代码:

def login(browser):
    username = 'Username'
    password = 'password'

    #Here is the first login button that is needed to be clicked before i'm gonna enter my data
    login = browser.find_element_by_xpath("""//*[@id="landing-page-modal"]/div/div[2]/div[1]/p[2]/a""")
    login.click()
    sleep(5)

    #Here is the place i enter my username
    username_input = browser.find_element_by_xpath("""//*[@id="js-login-form"]/div[1]/div[1]/input""")
    username_input.click()
    username_input.send_keys(username)

    #Here is the place i enter my password
    password_input = browser.find_element_by_xpath("""//*[@id="js-login-form"]/div[2]/div[1]/input""")
    password_input.click()
    password_input.send_keys(password)

    #Here i'm clicking the login button to login to my account
    login_button = browser.find_element_by_xpath("""//*[@id="js-login-button"]""")
    login_button.click()

这是我得到的错误:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="js-login-form"]/div[2]/div[1]/input"}
  (Session info: chrome=87.0.4280.88)

用户名和密码 xpath 都会出现问题。如果有人有解决方案,我将不胜感激。

该网站的链接是: https ://en.stoiximan.gr/

标签: pythonseleniumselenium-webdriverxpath

解决方案


推荐阅读