代码之家  ›  专栏  ›  技术社区  ›  user28864790

无法使用Python中的Selenium Webdriver在Chrome中登录网站

  •  -1
  • user28864790  · 技术社区  · 6 月前

    我正在使用Python中的Selenium模块登录网站 www.value,researchonline.com. .

    我可以点击“使用密码登录”按钮,然后执行代码输入用户名。但是,当我单击“提交”按钮时,它会显示错误“无法处理您的请求。请重试。”。我认为这是由于网站阻止了登录的自动脚本。代码粘贴在下面。

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    import time
    import pandas as pd
    import requests
    from bs4 import BeautifulSoup
    
    # Set up the Chrome WebDriver
    driver = webdriver.Chrome()
    
    url = "https://www.valueresearchonline.com/login/?site-code=VROL&target=%2F&utm_source=home&utm_medium=vro&utm_campaign=desktop-profile-menu/"
    driver.get(url)
    driver.implicitly_wait(5)
    
    # Click the button 'Login with password' (step 1)
    login_button1 = driver.find_element(By.CSS_SELECTOR, "button[data-user='Log in with password']")
    login_button1.click()
    
    # Define login credentials
    username = '[email protected]'
    # Find the username input field and submit button (step 2)
    username_field = driver.find_element(By.NAME, 'username')
    # Enter the username into the field
    username_field.send_keys(username)
    
    login_button2 = driver.find_element(By.CSS_SELECTOR, "button[id='proceed-btn']")
    # Submit the username to go to the password page
    login_button2.click()
    

    我检查了stackoverflow上的一些相关线程,但它们中的大多数都没有解决方案,或者提出的解决方案对我不起作用。请告知是否有任何解决方法。

    谢谢你的帮助。

    1 回复  |  直到 6 月前
        1
  •  0
  •   Explants    6 月前

    我无法发表评论,所以我将只使用答案。。。

    您可以尝试使用未检测到的Chromedriver吗?它至少会在你打开网站时解决验证码问题。我运行了代码,但我不确定您要查找的输出是什么。如果你能指导我,我可以试着帮助更多的人。这是代码

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    import undetected_chromedriver as uc
    import time
    import pandas as pd
    import requests
    from bs4 import BeautifulSoup
    
    
    url = "https://www.valueresearchonline.com/login/?site-code=VROL&target=%2F&utm_source=home&utm_medium=vro&utm_campaign=desktop-profile-menu/"
    
    options = uc.ChromeOptions()
    options.headless = False
    driver = uc.Chrome(options=options)
    
    driver.get(url)
    driver.implicitly_wait(5)
    
    # Click the button 'Login with password' (step 1)
    login_button1 = driver.find_element(By.CSS_SELECTOR, "button[data-user='Log in with password']")
    login_button1.click()
    
    # Define login credentials
    username = '[email protected]'
    # Find the username input field and submit button (step 2)
    username_field = driver.find_element(By.NAME, 'username')
    # Enter the username into the field
    username_field.send_keys(username)
    
    login_button2 = driver.find_element(By.CSS_SELECTOR, "button[id='proceed-btn']")
    # Submit the username to go to the password page
    login_button2.click()