I'm trying to perform google search using selenium python, but I can't seem to click the google search button with it. I am trying with the below code
browser = webdriver.Chrome(ChromeDriverManager().install())
url = 'https://google.com'
browser.get(url)
time.sleep(2)
name = 'q'
search_el = browser.find_element_by_name("q")
search_el.send_keys("selenium python")
submit_btn_el = browser.find_element_by_css_selector("input[type='submit']")
print(submit_btn_el.get_attribute('name'))
time.sleep(2)
submit_btn_el.click()
It fills the search bar with the search string selenium python correctly but an exception while clicking the button:
[24660:18480:0504/185929.817:ERROR:configuration_policy_handler_list.cc(90)] Unknown policy: EnableCommonNameFallbackForLocalAnchors
[24660:18480:0504/185929.881:ERROR:configuration_policy_handler_list.cc(90)] Unknown policy: EnableCommonNameFallbackForLocalAnchors
DevTools listening on ws://127.0.0.1:58996/devtools/browser/7031edca-5982-4156-b093-e03f85607449
[24660:18480:0504/185929.983:ERROR:browser_switcher_service.cc(238)] XXX Init()
Traceback (most recent call last):
File "c:/Users/apugazhenthy/Documents/30 days of python/Day 16/google.py", line 18, in <module>
WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"input[type='submit']"))).click()
File "C:\Program Files (x86)\Python38-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in
click
self._execute(Command.CLICK_ELEMENT)
File "C:\Program Files (x86)\Python38-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "C:\Program Files (x86)\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in
execute
self.error_handler.check_response(response)
File "C:\Program Files (x86)\Python38-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242,
in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <input class="gNO89b" value="Google Search" aria-label="Google Search" name="btnK" type="submit" data-ved="0ahUKEwjbtaqk15rpAhVExzgGHS16BW0Q4dUDCAc"> is not clickable at point (431, 521). Other element would receive the click: <div class="fbar">...</div>
(Session info: chrome=81.0.4044.129)
And this is the HTML form code from the web:
<input class="gNO89b" value="Google Search" aria-label="Google Search" name="btnK" type="submit" data-ved="0ahUKEwjHw5eTuprpAhXGjKQKHd7SCZkQ4dUDCAs">
Using RETURN key seems to be working :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
browser = webdriver.Chrome()
browser.get('https://google.com')
browser.find_element_by_name("q").send_keys("selenium python"+Keys.RETURN)
Did you check to see if the search button is located within a different iframe? When using Selenium, if the item you want to access is located within a different iframe, you have to switch to that iframe first, and then you can access the item. you can use switchTo() for that.
Induce a WebDriverWait before clicking the button:
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
browser = webdriver.Chrome()
url = 'https://google.com'
browser.get(url)
name = 'q'
search_el = browser.find_element_by_name("q")
search_el.send_keys("selenium python")
WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.XPATH,"//div[#class='aajZCb']//input[#value='Google Search']"))).click()
#submit_btn_el = browser.find_element_by_css_selector("input[type='submit']")
#print(submit_btn_el.get_attribute('name'))
Related
I am trying to accept cookies and search in google. But I am facing a problem that I never faced before.
Webdriver cant find the accept cookies button element. I looked everything. I tried to see if something changes the xpath(Is there any thing triggers any other element so the xpath will change) but I have zero in my hand. I tried using accept.send_keys(Keys.RETURN) and accept.click(). Nothing seems to work.
What I have for now;
def GoogleIt():
path = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(path)
driver.get("https://google.com")
wait(5)
accept = driver.find_element_by_xpath("/html/body/div/c-wiz/div[2]/div/div/div/div/div[2]/form/div/div[2]")
accept.send_keys(Keys.RETURN)
accept.click()
search = driver.find_element_by_xpath("/html/body/div/div[2]/form/div[2]/div[1]/div[1]/div/div[2]/input")
search.click()
search.send_keys(talk)
Note: wait() is a different name of time.sleep()
Error;
Traceback (most recent call last):
File "C:\Users\Teknoloji\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "C:/Users/Teknoloji/Desktop/Phyton/Assistant V1/Assistant.py", line 20, in GoogleIt
accept = driver.find_element_by_xpath("/html/body/div/c-wiz/div[2]/div/div/div/div/div[2]/form/div/div[2]")
File "C:\Users\Teknoloji\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Users\Teknoloji\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\Teknoloji\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\Teknoloji\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div/c-wiz/div[2]/div/div/div/div/div[2]/form/div/div[2]"}
(Session info: chrome=85.0.4183.121)
Process finished with exit code -1
If you need more information I am here.
Thanks...
To click on accept cookies button which is inside an iframe you need to switch to iframe first.
Induce WebDriverWait() and frame_to_be_available_and_switch_to_it() and following css selector.
Induce WebDriverWait() and element_to_be_clickable() and following xpath.
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://consent.google.com']")))
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//div[#id='introAgreeButton']"))).click()
You need to import following libraries.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
In case KunduK's solution doesn't work for some of those reading this, use "By.ID" parameter instead of the "By.CSS_SELECTOR" in the frame_to_be_available_and_switch_to_it() method -
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID, "name_of_the_iframe")))
After the latest update of Selenium, the above solutions did not work for me.
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium import webdriver
from selenium.webdriver.common.by import By
firefox_binary = FirefoxBinary()
browser = webdriver.Firefox(firefox_binary=firefox_binary)
Let's say you want to get this page from Google:
browser.get('https://www.google.com/search?q=cats&source=lnms&tbm=isch')
To continue, you have to click on "Accept all"
Then, you use Selenium to click the button:
browser.find_element(By.XPATH,"//.[#aria-label='Accept all']").click()
Then, you can continue to Google page!
I am attempting to try and type into a login page using Selenium to go to the page and click on the login box and type in some text. My code allows me to go to the login page, click the login box, and this is the part where my code breaks.
Code
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument("--test-type")
driver = webdriver.Chrome(executable_path = r'C:\Users\user\Downloads\chromedriver_win32\chromedriver.exe')
driver.get("https://accounts.google.com/signin/v2/identifier?passive=1209600&continue=https%3A%2F%2Fdocs.google.com%2F&followup=https%3A%2F%2Fdocs.google.com%2F&emr=1&flowName=GlifWebSignIn&flowEntry=ServiceLogin")
text_area= driver.find_element_by_xpath('//*[#id="view_container"]/div/div/div[2]/div/div[1]/div/form/span/section/div/div/div[1]/div/div[1]/div/div[2]')
text_area.click()
text_area.send_keys(email_address)
The code opens the page, which in case anyone is wondering or if this might be what is affecting my code is the login page when you go to Google Docs on a guest account, clicks on the login text box, and fails to type any text. At the point in time that the code should be typing in text, I get this error.
Error
Traceback (most recent call last):
File "file.py", line 8, in <module>
text_area.click()
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <div class="i9lrp mIZh1c"></div> is not clickable at point (508, 242). Other element would receive the click: <input type="email" class="whsOnd zHQkBf" jsname="YPqjbf" autocomplete="username" spellcheck="false" tabindex="0" aria-label="Email or phone" name="identifier" value="" autocapitalize="none" id="identifierId" dir="ltr" data-initial-dir="ltr" data-initial-value="">
(Session info: chrome=81.0.4044.92)
Admittedly this is a bit over my head and I am wondering if anyone might know how to fix this error, and how they came to find out how to do so since my next reasonable steps would be to type in the password, select new document, and type text into the Google Doc.
You should try to set the value in input but instead your current code is getting div element and inputbox is intercepting the click on the div element. Instead please, try with the below line of code.
text_area= driver.find_element_by_xpath("//input[#name='identifier']")
You can use waits, i.e.:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(executable_path="C:\Users\user\Downloads\chromedriver_win32\chromedriver.exe")
driver.maximize_window()
driver.get("https://accounts.google.com/signin/v2/identifier?passive=1209600&continue=https%3A%2F%2Fdocs.google.com%2F&followup=https%3A%2F%2Fdocs.google.com%2F&emr=1&flowName=GlifWebSignIn&flowEntry=ServiceLogin")
wait = WebDriverWait(driver, 10)
# usr
el = wait.until(EC.visibility_of_element_located((By.ID, "identifierId")))
el.send_keys("username#gmail.com")
el.send_keys(Keys.ENTER)
# pwd
el = wait.until(EC.visibility_of_element_located((By.XPATH, "//input[#type='password']")))
el.send_keys("XXXXX")
el.send_keys(Keys.ENTER)
# you're logged
im trying to click a button on a page after logging in the button is the following HTML
<div id="carrierDashboardControls">
<button class="yms-button-primary" ng-click="refresh()">
<t>Refresh</t>
</button>
<button class="yms-button-primary-alt ng-isolate-scope" ng-csv="fetchData()" lazy-load="true"
csv-header="getCsvHeader" filename="carrier-dashboard.csv" field-separator=",">CSV
</button>
</div>
there are 2 buttons in this and i want to click the one with class
"yms-button-primary-alt ng-isolate-scope"
however i get the follwing error
this button will download a CSV file when clicked but right now i get the error "selenium.common.exceptions.WebDriverException: Message: Failed to convert data to an object"
im currently using the below code, note the actual url's cannot be shared due to the nature of the business (i navigate to the url twice due to a redirection after login)
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
import re
import pandas as pd
import os
url = "THE URL"
username = 'USERNAME'
password = 'PASSWORD'
driver = webdriver.Firefox(executable_path=r'MYPATH/geckodriver.exe')
driver.implicitly_wait(100)
driver.get(url)
user_field = driver.find_element_by_id("ap_email")
pass_field = driver.find_element_by_id("ap_password")
sign_in = driver.find_element_by_id("signInSubmit")
user_field.send_keys(username)
pass_field.send_keys(password)
sign_in.click()
driver.implicitly_wait(100)
driver.get(url)
CSV_BUTTON = driver.find_element_by_class_name("yms-button-primary-alt ng-isolate-scope")
CSV_BUTTON.click()
as an added note i would like to manipulate the file that is downloaded afterwards as i would like to have it auto renamed with current date and time if this is possible ?
FULL STACKTRACE BELOW
Traceback (most recent call last):
File "C:/Users/USER/PycharmProjects/YMS scrape/venv/YMS Sel#.py", line 26, in <module>
CSV_BUTTON = driver.find_element_by_class_name("yms-button-primary-alt ng-isolate-scope")
File "C:\Users\USER\Anaconda3\envs\YMS scrape\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 564, in find_element_by_class_name
return self.find_element(by=By.CLASS_NAME, value=name)
File "C:\Users\USER\Anaconda3\envs\YMS scrape\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
'value': value})['value']
File "C:\Users\USER\Anaconda3\envs\YMS scrape\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\USER\Anaconda3\envs\YMS scrape\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Failed to convert data to an object
The desired element is an dynamic element which becomes visible through lazy-loading, so to click() on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.yms-button-primary-alt.ng-isolate-scope[csv-header='getCsvHeader'][ng-csv^='fetchData']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='yms-button-primary-alt ng-isolate-scope' and #csv-header='getCsvHeader'][contains(., 'CSV')]"))).click()
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
The element where I want to replace text:
<input tabindex="1" style="padding-left:119px!important;width:318px!important;background-color:#fff;outline: none;box-sizing: inherit;" id="email" name="mobile" placeholder="Enter your Mobile Number" class="un_s un1_s" value="" onblur="remove_border();" type="text" maxlength="20">
I have tried various ways(all I could think up and google), to replace the placeholder text. Lots of errors later, I thought since the focus is already in the text box(where I want to enter the text may be just send_keys would work. It didn't. Can someone help me out and explain the concept or point to where I might read on where I went wrong?
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
chrome_path = r"C:\Users\-------\Desktop\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get("SAMPLE WEBSITE URL") #sorry had to remove the link :(
driver.maximize_window()
action = webdriver.ActionChains(driver)
elm = driver.find_element_by_id("user_sign_in").click()
inputElement = driver.find_element_by_xpath('//*[#id="lfm"]/div[1]/div[2]') #driver.find_elements_by_xpath("//*[contains(text(), 'Enter your Mobile Number')]")
#driver.find_element_by_id("mobile")
inputElement.send_keys('1234567890')
inputElement.submit()
#//*[#id="lfm"]/div[1]/div[2] xpath id for the mobile number element
#code below this is not working, for move mouse
#action = webdriver.ActionChains(driver)
#action.move_to_element((By.XPATH, '//*[#id="user_sign_in"]')).perform()
#For moving the mouse to sign in: Tried the ones below and they didn't work either
#driver.move_to_element(By.XPATH, '//*[#id="user_sign_in"]')
#login_menu = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//*[#id="user_sign_in"]')))
#ActionChains(driver).move_to_element(sign_in).perform()
Errors:
Traceback (most recent call last):
File "C:/Python34/Selenium 2nd Trial.py", line 16, in
inputElement.send_keys('9810307369')
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webelement.py", line 322, in send_keys
self._execute(Command.SEND_KEYS_TO_ELEMENT, {'value': keys_to_typing(value)})
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webelement.py", line 457, in _execute
return self._parent.execute(command, params)
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 233, in execute
self.error_handler.check_response(response)
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot focus element
(Session info: chrome=55.0.2883.87)
(Driver info: chromedriver=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9),platform=Windows NT 10.0.14393 x86_64)
You are trying to send keys to the div element, but meant to use an input element:
inputElement = driver.find_element_by_id('email')
inputElement.send_keys('1234567890')
inputElement.submit()
And, be aware that the page might take some time to load and there could be visual effects preventing an element to be located or interactable. If this is the case, use WebDriverWait with an appropriate Expected Condition to make sure element is visible.
I am trying to browse facebook via selenium in python.
Here is my script so far.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
usr = ""# I have put 3 different accounts and tested it. Same error
pwd = ""
driver = webdriver.Chrome('E:\python_libs\chromedriver.exe')
driver.get("https://www.facebook.com")
assert "Facebook" in driver.title
elem = driver.find_element_by_id("email")
elem.send_keys(usr)
elem = driver.find_element_by_id("pass")
elem.send_keys(pwd)
elem.send_keys(Keys.RETURN)
elem = driver.find_element_by_css_selector(".input.textInput")
elem.send_keys("Posted using Python's Selenium WebDriver bindings!")
elem = driver.find_element_by_css_selector(".selected")
elem.click()
time.sleep(5)
driver.close()
This script when run on windows with chromedriver obtained and correctly installed returns an error.
Traceback (most recent call last):
File "C:\Users\Home\Desktop\facebook_post.py", line 28, in <module>
elem.click()
File "C:\Python27\lib\site-packages\selenium-2.42.0- py2.7.egg\selenium\webdriver\remote\webelement.py", line 60, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Python27\lib\site-packages\selenium-2.42.0- py2.7.egg\selenium\webdriver\remote\webelement.py", line 370, in _execute
return self._parent.execute(command, params)
File "C:\Python27\lib\site-packages\selenium-2.42.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 172, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium-2.42.0-py2.7.egg\selenium\webdriver\remote\errorhandler.py", line 164, in check_response
raise exception_class(message, screen, stacktrace)
WebDriverException: Message: u'unknown error: Element is not clickable at point (481, 185). Other element would receive the click: <input type="file" class="_n _5f0v" title="Choose a file to upload" accept="image/*" name="file" id="js_0">\n (Session info: chrome=35.0.1916.114)\n (Driver info: chromedriver=2.10.267521,platform=Windows NT 6.1 x86)'
I am unable to make head or tail of this.
Any help would be appreciated.
The Graph API is not feasible here as I want to browse as myself and not as some application.
If however browsing as myself can be done by graph API or some other means, please do tell.
If you're doing this process several times, Selenium has some options in WebDriverWait to not waste too much time and check to see if elements are visible.
Here is the documentation on Waits in Selenium and
this is a section of the Python documentation of Selenium that talks about the Expected Conditions classes. It seems that "clickable" is one conditions that you can check for.
In my case, I wrote a simple function that took care of visibility and clicking and called it every time I needed to click on something dynamic.
My example code:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Browser = webdriver.Chrome()
def wait_until_visible_then_click(element):
element = WebDriverWait(Browser,5,poll_frequency=.2).until(
EC.visibility_of(element))
element.click()
EDIT:
The links above seem to be nerfed. This is the new documentation on waits and here are the expected conditions docs.