im trying to write a code to automatically create a gmail using selenium. the item im trying to find is the firstname input in the url below:
https://accounts.google.com/signup/v2/webcreateaccount?service=mail&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&flowName=GlifWebSignIn&flowEntry=SignUp
i used wait but it returns a TimeoutException error.
also, say i want to use implicit wait, how can i do that?
thanks
class BotCreator:
def __init__(self, firstname, lastname):
self.firstname = firstname
self.lastname = lastname
self.driver = webdriver.Firefox(executable_path=r'A:\Python Projects\The InstaBOT/geckodriver')
def shutdown(self):
self.driver.close
def gmail_creator(self, n_bots):
for n in range(n_bots):
global email
email = {}
driver = self.driver
wait = WebDriverWait(driver, 10)
driver.get('https://www.google.com/intl/en-GB/gmail/about/')
driver.find_element_by_xpath('//a[#title="Create an account"]').click()
wait.until(ec.new_window_is_opened(driver.window_handles))
after = driver.window_handles[1]
driver.switch_to.window(after)
element = wait.until(ec.element_to_be_clickable((By.XPATH, '//input[#id="firstName"]')))
element.send_keys('awsd')
return email
gmail_t = BotCreator('John', 'Hoffinsky')
gmail_t.gmail_creator(1)
There are actually several (4) elements with the same XPath on that page.
Try to use a specific one with following XPath:
(//a[#title="Create an account"])[1]
Your XPath returns 4 elements with the Chrome extension ChroPath.
Regarding your question about implicit wait:
You are already using implicit wait. The implicit wait setting tells Selenium to poll for the specified amount of time until an element is available. You can change the timeout if needed:
driver.implicitly_wait(15)
This link could be helpful.
Once you click on the create Account button it opens a new window for user details.You need to Switch to that window to access the element.Try below code.
wait = WebDriverWait(driver, 10)
driver.get('https://www.google.com/intl/en-GB/gmail/about/')
driver.find_element_by_xpath('//a[#title="Create an account"]').click()
wait.until(ec.new_window_is_opened(driver.window_handles))
after=driver.window_handles[1]
driver.switch_to.window(after)
element = wait.until(ec.element_to_be_clickable((By.XPATH, '//input[#id="firstName"]')))
element.send_keys('awsd')
Browser snapshot:
Is always good to use ID than XPath/CSS
Example
driver.find_element_by_id("FirstName").send_keys("email")
Related
I am trying to search a name in linked in and then select the first result, I have done almost everything except selecting the first result, I am unable to do that, i have tried with xpath(its only working for the profile u copy xpath not for different profile), but its not working, below is the code what I have tried. For now I am trying for only one profile, but it should work for many profiles. So I need a better solution. Please help.
driver = webdriver.Chrome(r'your chromedriver path\chromedriver.exe')
driver.get("linked_in sign in page url")
username = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='session_key']")))
password = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='session_password']")))
# enter username and password
username.clear()
username.send_keys("give your user name")
password.clear()
password.send_keys("your password")
# target the login button and click it for login
button = WebDriverWait(driver, 2).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type='submit']"))).click()
# find the search bar and enter name for search
search = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[placeholder='Search']")))
search.clear()
search.send_keys("name of person")
search.send_keys(Keys.ENTER)
# select the first result after search
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,'//*[#id="main"]/div/div/div[1]/div/a/div'))).click()
Instead of that crazy xpath, it works quite well for me to simply use the following: $$('a.app-aware-link')
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'a.app-aware-link'))).click()
I am trying to write a program to automatically log into WSJ.com with my own login. I've looked at some of the other guy's codes, but none of them seemed to have the problem where when click the Login link on the WSJ.com main page, it takes you to two potential pages:
Ask for user name and password.
Ask for user name, once hit continue brings you to the page mentioned above.
For the 2nd scenario, I have managed to input username and password, then hit login. However that brings me to the next page where it askes if I want to verify my email or continue to WSJ.
In my code, I can't seem to get the webdriver to locate the button that says "Continue to WSJ". If I hit F12 under developer tools, I can locate the button.
Code below:
try:
submit_button = driver.find_element_by_xpath(".//button[#type='button'][#class='solid-button continue-submit new-design']")
username = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, 'username')))
user1 = "username"
pass1 = "password"
username.send_keys(user1)
submit_button = driver.find_element_by_xpath(".//button[#type='button'][#class='solid-button continue-submit new-design']")
submit_button.click()
password = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, 'password')))
password = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, 'password')))
username.send_keys(user1)
password.send_keys(pass1)
driver.get(url)
submit_button = driver.find_element_by_xpath(".//button[#type='button'][#class='solid-button new-design basic-login-submit']")
submit_button.click()
except:
username = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, 'username')))
password = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, 'password')))
user1 = "username"
pass1 = "password"
username.send_keys(user1)
password.send_keys(pass1)
submit_button = driver.find_element_by_xpath(".//button[#type='submit'][#class='solid-button basic-login-submit']")
submit_button.click()
continue_button = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CLASS_NAME, "solid-button reg-rtl-btn")))
continue_button.click()
For my code when exception occurs, especially trying to locate the "solid-button reg-rtl-btn" button, it gives me the below message:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".solid-button reg-rtl-btn"}
When stringing together classes in a class name selector, they need to get separated by ., which says the next thing coming is also a class name (the first one is automatically a class because you are searching by class).
Thus solid-button.reg-rtl-btn will locate by class name and .solid-button.reg-rtl-btn would locate by CSS selector, but having the space in between meant it did not know how to interpret reg-rtl-button. There are more complicated ways to do this, too, but I do not see why you would want to (maybe if you were worried about the order in which the classes could appear).
My Python Selenium can't click target element, instead seem click element behind target element?
I try to click, or enter text in 'drop-down meun', but I find that I am result in clicking element behind this 'drop-down. I know this is element behind it, because there is advistering area behind, and the result top-up showing the same advistering material. Here is my code:
# info for login
my_email = 'my_email'
my_passcode = 'my_passcode'
email_url = r'https://www.gmx.com/#.1559516-header-navlogin2-1'
# start driver and open url
driver = webdriver.Chrome(chrome_path)
driver.get(email_url)
# input email account
xpath = r'//*[#id="login-email"]'
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, xpath)))
target = driver.find_element_by_xpath(xpath)
actions = ActionChains(driver)
actions.move_to_element(target).perform()
actions.click().send_keys(my_email).perform()
# input passcode and hit 'enter' to login
xpath = r'//input[#id="login-password"]'
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, xpath)))
target = driver.find_element_by_xpath(xpath)
actions = ActionChains(driver)
actions.move_to_element(target).perform()
actions.click().send_keys(my_passcode).send_keys(Keys.ENTER).perform()
It happens to me for some other site when the site appear to have 'two layers' (not sure if I am using the right word). I can process anything on top layer, and only result in activate anything behind it. Many thank when provide solution!!
You don't need to use this ActionChains class, all you need to do can be done using WebElement.send_keys() and WebElement.click()
You don't need to re-find the element after using WebDriverWait as it returns the WebElement in case of success
I fail to see clickign on login button anywhere in your script, you can locate the relevant button using XPath contains() function like:
xpath = r'//button[contains(#class,"login-submit")]'
and then just call click() function on the resulting variable
Example suggested code:
You don't need to use this ActionChains class, all you need to do can be done using WebElement.send_keys() and WebElement.click()
You don't need to re-find the element after using WebDriverWait as it returns the WebElement in case of success
I fail to see clickign on login button anywhere in your script, you can locate the relevant button using XPath contains() function like:
xpath = r'//button[contains(#class,"login-submit")]'
and then just call click() function on the resulting variable
Example suggested code:
# input email account
xpath = r'//*[#id="login-email"]'
target = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, xpath)))
target.send_keys(my_email)
# input passcode and hit 'enter' to login
xpath = r'//input[#id="login-password"]'
target = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, xpath)))
target.send_keys(my_passcode)
xpath = r'//button[contains(#class,"login-submit")]'
target = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, xpath)))
target.click()
I have the following three buttons that I can't figure out how to grab the text that is inside of them (e.g Outliers). I tried browser.find_element_by_link_text("Outliers").click(), but got "Unable to locate element" error. How can I do it?
See: find_element_by_* commands are deprecated in selenium
In newer versions of selenium try:
from selenium.webdriver.common.by import By
browser.find_element(By.XPATH, '//button[text()="Outliers"]')
older versions of selenium:
browser.find_element_by_xpath('//button[text()="Outliers"]')
To update ALL of the older versions I found a nifty regex here, and then just fixup the import:
https://stackoverflow.com/a/70586710/2026508
There are two ways :
By using text() method:
browser.find_element(By.XPATH,'//button[text()="Outliers"]')
By using normalize-space() method:
browser.find_element(By.XPATH, '//button[normalize-space()="Outliers"]')
Note : It is always better to use normalize-space() method as it will work even if there are spaces present at the start of your text or at the end of text, because normalize-space() method trim the left and right side spaces
For More information on Normalize-space()
Try this XPath:
"//button[#class='three-state-item btn btn-default'][.='Outliers']".
This is the solution that worked for me:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
CHROME_DRIVER_PATH = Your Driver Path
USERNAME = YOUR USERNAME
PASSWORD = YOUR PASSWORD
SIMILAR_ACCOUNT = "idogsplanet"
class InstaFollower:
def __init__(self, driver_path):
self.driver = webdriver.Chrome(executable_path=driver_path)
def login(self):
self.driver.get("https://www.instagram.com/accounts/login/")
time.sleep(3)
username = self.driver.find_element_by_name("username")
username.send_keys(USERNAME)
password = self.driver.find_element_by_name("password")
password.send_keys(PASSWORD)
time.sleep(2)
login = self.driver.find_element_by_xpath('//*[#id="loginForm"]/div/div[3]/button/div')
login.click()
def find_followers(self):
time.sleep(5)
self.driver.get("https://www.instagram.com/" + SIMILAR_ACCOUNT + "/followers")
followers = self.driver.find_element_by_xpath('//*[#id="react-root"]/section/main/div/header/section/ul/li[2]/a')
followers.click()
time.sleep(1)
def follow(self):
all_buttons = self.driver.find_elements_by_xpath('//button[normalize-space()="Follow"]')
modal = self.driver.find_element_by_xpath('/html/body/div[5]/div/div/div[2]')
for button in all_buttons:
if button.text != "Follow":
pass
else:
button.click()
time.sleep(2)
time.sleep(10)
self.driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight", modal)
self.follow()
bot = InstaFollower(CHROME_DRIVER_PATH)
bot.login()
bot.find_followers()
bot.follow()
I'm trying to log in into Quora website.
On my local machine it runs perfectly.
But on SSH server (droplet on DigitalOcean) - no, I'm getting InvalidElementStateException
I've tried to focus on the element by send_keys(Keys.NULL), and got ElementNotVisibleException
Here is the code:
driver.get("https://www.quora.com/")
print("Logging...")
# gets email and password from json
with open('config.json') as f:
login_data = json.load(f)
email = login_data['email']
password = login_data['pass']
time.sleep(3)
email_field_xpath = "//div[#class='form_column']/input[#name='email']"
password_field_xpath = "//div[#class='form_column']/input[#name='password']"
# webdriver's going to wait max 10 seconds for email's field, password field, login button to display
email_field_element = WebDriverWait(driver, 10).until(
lambda driver: driver.find_element_by_xpath(email_field_xpath))
password_field_element = WebDriverWait(driver, 10).until(
lambda driver: driver.find_element_by_xpath(password_field_xpath))
email_field_element.send_keys(Keys.NULL)
email_field_element.clear()
email_field_element.send_keys(email)
password_field_element.send_keys(Keys.NULL)
password_field_element.clear()
password_field_element.send_keys(password)
login_button_xpath = "//input[#value='Login']"
# wait till element is clickable
login_button_element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, login_button_xpath)))
login_button_element.click()
print("Logged In.")
It may possible that screen resolution you are using at your local machine and on SSH server are different. May be the size of opened browser windows are different at both at local and on SSH server.
Try to change your local settings as SSH server's than you can figure out what is actual reason or just try the possible solutions given below.
Possible solutions by focusing on element first. Here, your element can be email_field_element or password_field_element:
1) First move to that element using ActionChains:
from selenium.webdriver.common.action_chains import ActionChains
actions = ActionChains(driver)
actions.move_to_element(element)
actions.perform()
OR
2) You can also use scroll up to that element. Like:
driver.execute_script("arguments[0].scrollIntoView(true);", element)
OR
3)Input data using java script.
driver.execute_script("document.getElementById('elementID').setAttribute('value', 'new value for element')" # you can use xpath as well
Hope it may help you.