Issue
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:
Solution
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" class="r-30o5oe r-1niwhzg r-17gur6a r-1yadl64 r-deolkf r-homxoj r-poiln3 r-7cikom r-1ny4l3l r-t60dpp r-1dz5y72 r-fdjqy7 r-13qz1uu" 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:
Answered By - undetected Selenium
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.