How to fix selenium "Message: no such element: Unable to locate element - python-3.x

I need to fix my script I'm still geting the error:
no such element found
Part of my code is:
driver = webdriver.Chrome(r'C:\\chromedriver_win32\\chromedriver.exe')
driver.get("http://bulksmsplans.com/register")
# find username/email field and send the username itself to the input field
country_input = driver.find_element(By.NAME, "country_id")
select_object = Select(country_input)
select_object.select_by_value(country_id)
# find password input field and insert password as well
name_input = driver.find_element(By.NAME, "name")
time.sleep(2)
name_input.send_keys(name)
email_input = driver.find_element(By.NAME, "email")
time.sleep(2)
email_input.send_keys(email)
phone_input = driver.find_element(By.NAME, "phone")
phone_input.send_keys(phone)
time.sleep(2)
WebDriverWait(driver, 20).until(
EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[title='reCAPTCHA']")))
WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, "div.recaptcha-checkbox-border"))).click()
button = driver.find_element(By.XPATH, "//input[#type='submit']")
button.click();
All working, but the last step when the bot must to click on green button "Create Account", nope...
What is wrong on this line:
button = driver.find_element(By.XPATH, "//input[#type='submit']")
Thanks

While interacting with the reCAPTCHA you have switched to the <iframe>.
Next, to click on the element Create Account you have to shift Selenium's focus back to the default content (or parent_frame) using either of the following lines of code:
Using default_content():
driver.switch_to.default_content()
Using parent_frame():
driver.switch_to.parent_frame()

Related

Why is selenium not applying "click" to a popup?

I know the xpath of this popup tabs element is correct, however when I do filters_language_dropdown.click() and then .send_keys(Keys.Enter. It doesn't do anything.
However the same popup (press 'filters' button on this page to view) works with the xpath of the initial button press element instead (see code + images below) so with filters_button.send_keys.... Whats going on?
# Initialize the browser and navigate to the page
browser = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
browser.get("https://www.facebook.com/ads/library/?active_status=all&ad_type=all&country=ALL&q=%22%20%22&sort_data[direction]=desc&sort_data[mode]=relevancy_monthly_grouped&search_type=keyword_exact_phrase&media_type=all")
# (In working order): Look for keyword, make it clickable, clear existing data in box, enter new info, keep page open for 10 seconds
search_box = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[#placeholder='Search by keyword or advertiser']")))
search_box.click()
search_box.clear()
search_box.send_keys("" "" + Keys.ENTER)
time.sleep(3)
# Activating the filters (English, active ads, date from (last 2 days) to today)
filters_button = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, "//body[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[5]/div[2]/div[1]/div[1]/div[3]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]")))
filters_button.click()
filters_button.send_keys(Keys.ENTER)
time.sleep(3)
filters_language_dropdown = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[#id='js_knd']//div[#class='x6s0dn4 x78zum5 x13fuv20 xu3j5b3 x1q0q8m5 x26u7qi x178xt8z xm81vs4 xso031l xy80clv xb9moi8 xfth1om x21b0me xmls85d xhk9q7s x1otrzb0 x1i1ezom x1o6z2jb x1gzqxud x108nfp6 xm7lytj x1ykpatu xlu9dua x1w2lkzu']")))
filters_language_dropdown.click()
filters_language_dropdown.send_keys(Keys.ENTER)
time.sleep(3)
Use following xpath to click on the filter and then click on all languages and then click on English if you want to change other language you need to pass let say 'French',you need to change instead of English.
Code:
browser.get("https://www.facebook.com/ads/library/?active_status=all&ad_type=all&country=ALL&q=%22%20%22&sort_data[direction]=desc&sort_data[mode]=relevancy_monthly_grouped&search_type=keyword_exact_phrase&media_type=all")
search=WebDriverWait(browser,10).until(EC.visibility_of_element_located((By.XPATH,"//input[#placeholder='Search by keyword or advertiser']")))
search.click()
search.clear()
search.send_keys("" "" + Keys.ENTER)
WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.XPATH,"//div[text()='Filters']"))).click()
WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.XPATH,"//div[text()='All languages']"))).click()
WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.XPATH,"//div[text()='English']"))).click()
time.sleep(10) # to check the operation
Browser snapshot:

Getting error message with using find_element_by_name from Selenium

I am using selenium to automate few tasks. My code till now just opens browser and gets username and password to fill them later but find_element_by_name is not working properly.
from selenium import webdriver
import time
driver = webdriver.Chrome(r'C:\Users\tarek\Desktop\namshi\chromedriver.exe')
driver.get("https://my.namshi.com/")
time.sleep(5)
try:
username = driver.find_element_by_name('email')
password = driver.find_element_by_name('password')
print(username)
except:
print('failed')
Why would you need to get the password or email? Do you mean you want to click on the fields and fill them with the two queries? If so, then you need to call the click method on your username and password because the find_element_by_name function returns a web element instead of finding it (which would be way more convenient, to be honest)
Basically:
def log_in(email, password) -> Webelement (a class):
username = driver.find_element_by_name('email')
username.click()
username.send_keys(email, Keys.RETURN)
password = driver.find_element_by_name('password')
password.click()
password.send_keys(password, Keys.RETURN)
try:
log_in()
except:
print('failed')
You have at least three elements with name email, I have tried this XPath:
//*[#name='email']
Your element is the second one I guess:
(//input[#name='email'])[2]

Can't find element Selenium

I am trying to fill in the forms in the Gmail creation page. But for some reason, my driver can't find the code. I've used all the options, path, id, name, class name, but nothing works
chrome_driver = './chromedriver'
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=chrome_driver)
driver.get('https://www.google.com/intl/nl/gmail/about/#')
try:
print('locating create account button')
create_account_button = driver.find_element_by_class_name('h-c-button')
except:
print("error, couldn't find create account button")
try:
create_account_button.click()
print('navigating to creation page')
except:
print('error navigating to creation page')
time.sleep(15)
first_name_form = driver.find_element_by_class_name('whsOnd zHQkBf')
(the sleep is just temporary, to make sure it loads completely, I know it's not efficient)
here's the link to the Gmail page:
https://accounts.google.com/signup/v2/webcreateaccount?service=mail&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F%3Fpc%3Dtopnav-about-n-en&flowName=GlifWebSignIn&flowEntry=SignUp
This is the error I'm getting:
Exception has occurred: NoSuchElementException
Message: no such element: Unable to locate element: {"method":"css
selector","selector":".whsOnd zHQkBf"}
(Session info: chrome=81.0.4044.129)
Thank you for your help
I found your error and I have the solution for you. Let me first determine to you the problem. When you click on the 'Create New Account' a new window is opening. BUT your bot is still undestanding that you are located in the first window (in the one that you click on the first button in order to create the account). Thus, the bot is trying to see if there is a First Name input. That's why is failing. So, the solution is that you have to change the window that you want to specify. The way you can do it is written inside the code block.
CODE
from selenium import webdriver
import time
path = '/home/avionerman/Documents/stack'
driver = webdriver.Firefox(path)
driver.get('https://www.google.com/intl/nl/gmail/about/#')
try:
print('locating create account button')
create_account_button = driver.find_element_by_class_name('h-c-button')
except:
print("error, couldn't find create account button")
try:
create_account_button.click()
print('navigating to creation page')
except:
print('error navigating to creation page')
time.sleep(15)
# Keeping all the windows into the array named as handles
handles = driver.window_handles
# Keeping the size of the array in order to know how many windows are open
size = len(handles)
# Switch to the second opened window (id:1)
driver.switch_to.window(handles[1])
# Print the title of the current page in order to validate if it's the proper one
print(driver.title)
time.sleep(10)
first_name_input = driver.find_element_by_id('firstName')
first_name_input.click()
first_name_input.send_keys("WhateverYouWant")
last_name_input = driver.find_element_by_id('lastName')
last_name_input.click()
last_name_input.send_keys("WhateverYouWant2")
username_input = driver.find_element_by_id('username')
username_input.click()
username_input.send_keys('somethingAsAUsername')
pswd_input = driver.find_element_by_name('Passwd')
pswd_input.click()
pswd_input.send_keys('whateveryouwant')
pswd_conf_input = driver.find_element_by_name('ConfirmPasswd')
pswd_conf_input.click()
pswd_conf_input.send_keys('whateveryouwant')
time.sleep(20)
So, if you will go to line 21 you will see that I have some comments in order to tell you what these lines (from 21 until 31) are doing.
Also, I inserted all the needed code for you (first name, last name, et.c). You only have to locate the creation button (last one).
Note: Try to use ids in such cases and not class names (when the ids are clear and unique) as I already did for you.

How to send a tab key input into web-browser with selenium and python

I am trying to get this script to send a tab key input after inserting data into entry fields.
The reason is so the next set of data will be entered into the next row.
I've tried using the send_key method to send the tab button event to the browser, but I get an error. Is there a correct way to called a tab keyboard input based on this code sample?
from selenium.webdriver.common.keys import Keys
def GetQuote():
for i in range(len(orders)):
#description
driver.find_element(By.XPATH, "/html[1]/body[1]/app-root[1]/div[1]/div[1]/app-record[1]/div[1]/div[2]/div[1]/app-record-quoting[1]/div[1]/app-record-product-list-panel[1]/form[1]/div[3]/div[1]/div[1]/div[1]/input[1]").send_keys(orders[i]['description'])
#dropdown menu for Handling Unit
select_element = Select(driver.find_element_by_xpath('/html[1]/body[1]/app-root[1]/div[1]/div[1]/app-record[1]/div[1]/div[2]/div[1]/app-record-quoting[1]/div[1]/app-record-product-list-panel[1]/form[1]/div[3]/div[1]/div[3]/div[1]/select[1]'))
select_element.select_by_value('1')
driver.implicitly_wait(1)
driver.find_element(By.CSS_SELECTOR, "input[formControlName=handlingQty]").send_keys(orders[i]['handling unit'])
driver.find_element(By.CSS_SELECTOR, "input[formControlName=packageQty]").send_keys(orders[i]['pieces'])
driver.find_element(By.CSS_SELECTOR, "input[formControlName=length]").send_keys(orders[i]['length'])
driver.find_element(By.CSS_SELECTOR, "input[formControlName=width]").send_keys(orders[i]['width'])
driver.implicitly_wait(1)
driver.find_element(By.CSS_SELECTOR, "input[formControlName=height]").send_keys(orders[i]['height'])
element=driver.find_element(By.CSS_SELECTOR,"input[formControlName=weight]")
element.send_keys(orders[i]['weight'])
driver.implicitly_wait(1)
if i < len(orders):
element.send_keys(Keys.TAB);
driver.find_element(By.XPATH, "//button[#class='btn-filled clickable']").click() #generate quote button
Try to send the tab key to the element
element=driver.find_element(By.CSS_SELECTOR,"input[formControlName=weight]")
element.send_keys(orders[i]['weight'])
if i < len(orders):
element.send_keys(Keys.TAB);

Click same id buttom selenium PYTHON

from selenium import webdriver
import time
username = ""
password = ""
getdriver = ("https://www.instagram.com/accounts/login/")
driver = webdriver.Chrome()
driver.get(getdriver)
driver.find_element_by_xpath("//input[#name='username']").send_keys(username)
driver.find_element_by_xpath("//input[#name='password']").send_keys(password)
driver.find_element_by_xpath("//button[contains(.,'Log in')]").click()
time.sleep(5)
driver.get("https://www.instagram.com/python.coders/followers/")
driver.find_element_by_xpath('//*[#id="react-root"]/section/main/div/header/section/ul/li[2]/a').click()
while True:
time.sleep(2)
driver.find_element_by_name('Follow').click()
I want to click all Follow display buttom on instagram,i can not do it with tag_name or name.
Use findElements.
It returns a list of WebElements with same locator.
Now loop around the size of this list and keep on clicking the WebElements.

Resources