DeprecationWarning和NoSuchElementException在使用Selenium自动化Twitter登录时发生。

huangapple go评论123阅读模式
英文:

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(&quot;https://twitter.com/i/flow/login&quot;)

# Variables to store your credentials
email = &quot;your-email-address&quot;
password = &quot;your-password&quot;

# Copy the updated XPaths of the email field, password field, and login button
email_xpath = &#39;/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&#39;
password_xpath = &#39;/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&#39;
login_xpath = &#39;/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&#39;

# Find email field, password field, and login button using XPaths
email_field = driver.find_element(&#39;xpath&#39;, email_xpath)
password_field = driver.find_element(&#39;xpath&#39;, password_xpath)
login_button = driver.find_element(&#39;xpath&#39;, 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 &quot;C:\Users\adwai\OneDrive\Desktop\twitter- automation\app.py&quot;, line 19, in &lt;module&gt;
    email_field = driver.find_element(&#39;xpath&#39;, email_xpath)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File &quot;C:\Users\adwai\OneDrive\Desktop\twitter- automation\venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py&quot;, line 831, in find_element
    return self.execute(Command.FIND_ELEMENT, {&quot;using&quot;: by, &quot;value&quot;: value})[&quot;value&quot;]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File &quot;C:\Users\adwai\OneDrive\Desktop\twitter- automation\venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py&quot;, line 440, in execute
    self.error_handler.check_response(response)
  File &quot;C:\Users\adwai\OneDrive\Desktop\twitter- automation\venv\Lib\site-packages\selenium\webdriver\remote\errorhandler.py&quot;, line 245, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {&quot;method&quot;:&quot;xpath&quot;,&quot;selector&quot;:&quot;/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&quot;}
  (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(&quot;--start-maximized&quot;)
options.add_experimental_option(&quot;excludeSwitches&quot;, [&quot;enable-automation&quot;])

driver = webdriver.Chrome(options=options)
wait = WebDriverWait(driver, 20)
url = &quot;https://twitter.com/i/flow/login&quot;
driver.get(url)

username = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, &#39;input[autocomplete=&quot;username&quot;]&#39;)))
username.send_keys(&quot;YOUR USERNAME&quot;)
username.send_keys(Keys.ENTER)

password = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, &#39;input[name=&quot;password&quot;]&#39;)))
password.send_keys(&quot;YOUR PASSWORD&quot;)
password.send_keys(Keys.ENTER)

time.sleep(10)

huangapple
  • 本文由 发表于 2023年6月2日 04:09:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76385369.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定