英文:
DeprecationWarning and NoSuchElementException when automating Twitter login using Selenium
问题
我正在尝试使用Python中的Selenium自动化登录Twitter的过程。然而,我遇到了两个问题:一个是DeprecationWarning,另一个是NoSuchElementException。以下是我的代码:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time
# 自动化打开Twitter登录页面的过程
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://twitter.com/i/flow/login")
# 存储您的凭据的变量
email = "your-email-address"
password = "your-password"
# 复制更新后的电子邮件字段、密码字段和登录按钮的XPaths
email_xpath = '/html/body/div/div/div/div[1]/div/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div/div/div/div[5]/label/div/div[2]/div/input'
password_xpath = '/html/body/div/div/div/div[1]/div/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[1]/div/div/div[3]/div/label/div/div[2]/div[1]/input'
login_xpath = '/html/body/div/div/div/div[1]/div/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[2]/div/div[1]/div/div/div'
# 使用XPaths查找电子邮件字段、密码字段和登录按钮
email_field = driver.find_element('xpath', email_xpath)
password_field = driver.find_element('xpath', password_xpath)
login_button = driver.find_element('xpath', login_xpath)
email_field.send_keys(email)
password_field.send_keys(password)
time.sleep(1)
login_button.click()
然而,我收到了以下错误消息:
DeprecationWarning: executable_path has been deprecated, please pass in a Service object
driver = webdriver.Chrome(ChromeDriverManager().install())
Traceback (most recent call last):
File "C:\Users\adwai\OneDrive\Desktop\twitter- automation\app.py", line 19, in <module>
email_field = driver.find_element('xpath', email_xpath)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\adwai\OneDrive\Desktop\twitter- automation\venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 831, in find_element
return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\adwai\OneDrive\Desktop\twitter- automation\venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 440, in execute
self.error_handler.check_response(response)
File "C:\Users\adwai\OneDrive\Desktop\twitter- automation\venv\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 245, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div/div/div/div[1]/div/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div/div/div/div[5]/label/div/div[2]/div/input"}
(Session info: chrome=113.0.5672.127)
Stacktrace:
...
如何解决这些问题,以成功使用Selenium自动化Twitter登录过程?
英文:
I am trying to automate the process of logging into Twitter using Selenium in Python. However, I am encountering two issues: a DeprecationWarning and a NoSuchElementException. Here is my code:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time
# Automating the process of opening the Twitter login page
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://twitter.com/i/flow/login")
# Variables to store your credentials
email = "your-email-address"
password = "your-password"
# Copy the updated XPaths of the email field, password field, and login button
email_xpath = '/html/body/div/div/div/div[1]/div/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div/div/div/div[5]/label/div/div[2]/div/input'
password_xpath = '/html/body/div/div/div/div[1]/div/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[1]/div/div/div[3]/div/label/div/div[2]/div[1]/input'
login_xpath = '/html/body/div/div/div/div[1]/div/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[2]/div/div[1]/div/div/div'
# Find email field, password field, and login button using XPaths
email_field = driver.find_element('xpath', email_xpath)
password_field = driver.find_element('xpath', password_xpath)
login_button = driver.find_element('xpath', login_xpath)
email_field.send_keys(email)
password_field.send_keys(password)
time.sleep(1)
login_button.click()
However, I am getting the following error messages:
DeprecationWarning: executable_path has been deprecated, please pass in a Service object
driver = webdriver.Chrome(ChromeDriverManager().install())
Traceback (most recent call last):
File "C:\Users\adwai\OneDrive\Desktop\twitter- automation\app.py", line 19, in <module>
email_field = driver.find_element('xpath', email_xpath)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\adwai\OneDrive\Desktop\twitter- automation\venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 831, in find_element
return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\adwai\OneDrive\Desktop\twitter- automation\venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 440, in execute
self.error_handler.check_response(response)
File "C:\Users\adwai\OneDrive\Desktop\twitter- automation\venv\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 245, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div/div/div/div[1]/div/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div/div/div/div[5]/label/div/div[2]/div/input"}
(Session info: chrome=113.0.5672
.127)
Stacktrace:
...
How can I resolve these issues and successfully automate the Twitter login process using Selenium?
答案1
得分: 2
你可以尝试这种方式:
import time
from selenium import webdriver
from selenium.webdriver import ChromeOptions, Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
options = ChromeOptions()
options.add_argument("--start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
driver = webdriver.Chrome(options=options)
wait = WebDriverWait(driver, 20)
url = "https://twitter.com/i/flow/login"
driver.get(url)
username = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'input[autocomplete="username"]')))
username.send_keys("YOUR USERNAME")
username.send_keys(Keys.ENTER)
password = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'input[name="password"]')))
password.send_keys("YOUR PASSWORD")
password.send_keys(Keys.ENTER)
time.sleep(10)
英文:
You may try this way:
import time
from selenium import webdriver
from selenium.webdriver import ChromeOptions, Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
options = ChromeOptions()
options.add_argument("--start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
driver = webdriver.Chrome(options=options)
wait = WebDriverWait(driver, 20)
url = "https://twitter.com/i/flow/login"
driver.get(url)
username = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'input[autocomplete="username"]')))
username.send_keys("YOUR USERNAME")
username.send_keys(Keys.ENTER)
password = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'input[name="password"]')))
password.send_keys("YOUR PASSWORD")
password.send_keys(Keys.ENTER)
time.sleep(10)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论