What I want to do: Build a tweet bot for twitter.
My problem: My program is unable to find the text box to enter my username, even when I use explicit wait.
What I've tried:
- locating the element by class
- locating the element by name
- locating the element by xpath
- using explicit wait to make sure the element is on the screen before program continues.
My 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
# getting webdriver path
s = Service('/Users/shannonslater/Desktop/Everything/Dev/WebDrivers/chromedriver101')
driver = webdriver.Chrome(service=s)
# navigating to twitter login page
driver.get('https://twitter.com/login')
# trying to click on the username text box
try:
username_text_box = WebDriverWait(driver, 20).until(
EC.presence_of_element_located(By.XPATH, '//*[@id="layers"]/div/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[1]/div/div/div[5]/label/div/div[2]/div/input')
)
username_text_box.click()
except:
print("could not find username_text_box element")
# username text box is never clicked and "could not find username" is always printed
I am copying the xpath directly from the inspected html element:
CodePudding user response:
The username field on Twitter Login Page is a dynamic element.
<input autocapitalize="sentences" autocomplete="username" autocorrect="on" name="text" spellcheck="true" type="text" dir="auto" value="">
Solution
To send a character sequence to the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
options = Options() options.add_argument("start-maximized") s = Service('C:\\BrowserDrivers\\chromedriver.exe') driver = webdriver.Chrome(service=s, options=options) driver.get("https://twitter.com/i/flow/login") WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[autocomplete='username']"))).send_keys("ShannonSlater")
Using XPATH:
options = Options() options.add_argument("start-maximized") s = Service('C:\\BrowserDrivers\\chromedriver.exe') driver = webdriver.Chrome(service=s, options=options) driver.get("https://twitter.com/i/flow/login") WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@autocomplete='username']"))).send_keys("ShannonSlater")
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
Browser Snapshot: