Hello i am trying to build a program that logins the twitter automaticaly by using the username and password informations with selenium module. I am getting an error which basically says it cannot find the element. This is the code i wrote
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
username=""
password=""
driver=webdriver.Firefox(executable_path=r'C:\geckodriver-v0.31.0-win64\geckodriver.exe')
driver.get("https://twitter.com/i/flow/login")
usernameInput=driver.find_element(By.NAME,"text")
i also applied the same process by using xpath instead of name, it didin't work either. I am dropping a screenshot of where i got the name tag:
Error message:
raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [name="text"] Stacktrace: RemoteError@chrome://remote/content/shared/RemoteError.jsm:12:1 WebDriverError@chrome://remote/content/shared/webdriver/Errors.jsm:192:5 NoSuchElementError@chrome://remote/content/shared/webdriver/Errors.jsm:404:5 element.find/</<@chrome://remote/content/marionette/element.js:291:16
CodePudding user response:
It's a timing issue. Use WebDriverWait
as shown below:
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
options = webdriver.ChromeOptions()
options.add_argument("--disable-notifications")
options.add_experimental_option(
"excludeSwitches", ["enable-automation"],
)
prefs = {
"credentials_enable_service": False,
"profile.password_manager_enabled": False,
}
options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(options=options)
driver.get("https://twitter.com/i/flow/login")
element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, '[name="text"]'))
)
element.send_keys("MY_USERNAME")
time.sleep(3) # Long enough to see the name was typed
driver.quit()
CodePudding user response:
Can you try using this xpath:
//span[text()='Phone, email address, or username']
If this does not work, Then use the above Xpath and just click the field and do not pass text. Then in the next step, pass the value using:
driver.find_element(By.XPATH,"//span[text()='Phone, email address, or username']").click()
usernameInput=driver.find_element(By.NAME,"text").send_keys(username)
CodePudding user response:
Should just be simple to send keys to that input tag.
driver.find_element(By.XPATH,"//input[@autocomplete='username']").send_keys('user')