Python 3.10结合Selenium实现Twitter登录时的Driver初始化报错与元素定位失败问题
Hey there! Let’s work through your two issues one by one to get your login script up and running smoothly.
1. Fixing the executable_path has been deprecated Warning
Starting with Selenium 4, the old way of passing the ChromeDriver path directly to webdriver.Chrome() is no longer supported. Instead, you need to use the Service class to manage the driver executable properly.
Updated Driver Initialization Code
from selenium import webdriver from selenium.webdriver.chrome.service import Service 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 # Set up the Service with your local ChromeDriver path chrome_driver_path = "C:/Users/osman/OneDrive/Desktop/chromedriver.exe" service = Service(chrome_driver_path) driver = webdriver.Chrome(service=service) driver.get("https://www.twitter.com/login")
Pro tip: For a more hassle-free setup, use webdriver-manager to automatically handle ChromeDriver versions (no manual downloads or path updates needed). Install it with pip install webdriver-manager, then use:
from webdriver_manager.chrome import ChromeDriverManager service = Service(ChromeDriverManager().install()) driver = webdriver.Chrome(service=service)
2. Fixing NoSuchElementException for the Username Input
Your original XPath is an absolute path—this is super fragile for React-based sites like Twitter, where page structure elements (like div positions or dynamic IDs) often change. Plus, you’re trying to locate the element before the page finishes loading. Here’s the fix:
Use Stable Locators + Explicit Waits
Instead of relying on flaky absolute XPath, use a more reliable locator (like the element’s name attribute or placeholder text). Pair this with explicit waits to ensure the element is fully loaded before you interact with it.
Full Updated Login Script
from selenium import webdriver from selenium.webdriver.chrome.service import Service 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 chrome_driver_path = "C:/Users/osman/OneDrive/Desktop/chromedriver.exe" service = Service(chrome_driver_path) driver = webdriver.Chrome(service=service) driver.get("https://www.twitter.com/login") # Wait up to 10 seconds for the username input to become visible wait = WebDriverWait(driver, 10) username_input = wait.until(EC.visibility_of_element_located((By.NAME, "text"))) # Enter your email/username username_input.send_keys('osman@yahoo.com') # Optional: Press Enter to move to the password screen (or click the "Next" button) username_input.send_keys(Keys.ENTER) # If you want to complete the login, add password input here: # password_input = wait.until(EC.visibility_of_element_located((By.NAME, "password"))) # password_input.send_keys('your_password_here') # password_input.send_keys(Keys.ENTER)
Why This Works:
- Explicit Waits:
WebDriverWaitprevents race conditions where your script runs faster than the page loads, ensuring the element exists before you try to interact with it. - Stable Locators: Using
By.NAME, "text"targets the username/email field directly (Twitter’s input hasname="text"), which is way less likely to break than absolute XPath. - Alternative Locators: If
name="text"ever stops working, you can use:By.XPATH, "//input[@placeholder='Phone, email, or username']"(targets the placeholder text)By.CSS_SELECTOR, "input[autocomplete='username']"(uses the autocomplete attribute)
Bonus: Fixing the Greyed-Out Keys Import
The Keys import was greyed out because you weren’t using it in your original script. In the updated code above, we use Keys.ENTER to proceed after entering the username, so the import will no longer be greyed out.
内容的提问来源于stack exchange,提问作者Mohammad Osman

