Click automation using Selenium - python

For automating downloading from a website, I am using this python script for automating click action. But the click does not work.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
browser = webdriver.Firefox()
browser.maximize_window()
browser.implicitly_wait(20)
browser.get('http://download.cnet.com/most-popular/windows/')
linkElem = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "CCleaner")))
linkElem.click() # follows the "CCleaner" link
I first got this error:
selenium.common.exceptions.ElementNotInteractableException: Message: Element could not be scrolled into view
Then I add this line:
linkElem = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "CCleaner")))
Now I get this error:
linkElem = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "CCleaner")))
File "/usr/lib/python2.7/site-packages/selenium/webdriver/support/wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
I tried the things that people used to solve this problem but did not work.

Your code seems to work with Chrome, I only added the bottom two lines and it downloaded CCleaner.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
browser = webdriver.Chrome() # Firefox()
browser.maximize_window()
browser.implicitly_wait(20)
browser.get('http://download.cnet.com/most-popular/windows/')
linkElem = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "CCleaner")))
linkElem.click() # follows the "CCleaner" link
download = browser.find_element_by_id('download-now-btn-text')
download.click()

Related

Python + Selenium TimeoutException

This is my first time using Python and Selenium. The first part of the code works but when it goes to the second page it can never find any of the elements. If I flip the code and make it go to the second site first, it works. What am I doing wrong here? I tried xpath, CSS_Selector, Class_Name seems like nothing is working. This is the error I get:
Traceback (most recent call last):
File "C:\Users\dresd\PycharmProjects\Test2\main.py", line 20, in
click_Register = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CLASS_NAME, "q-text qu-ellipsis qu-whiteSpace--nowrap"))).click()
File "C:\Users\dresd\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\support\wait.py", line 89, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
import time
driver = webdriver.Chrome ("C:/chromedriver.exe")
driver.get("https://10minutesemail.net/")
Copy_Email = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.ID, "copyEmailAddress"))).click()
time.sleep(10)
driver.execute_script("window.open('https://quora.com/','_blank')")
click_Register = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.CLASS_NAME, "q-text qu-ellipsis qu-whiteSpace--nowrap"))).click()
name = driver.find_element(By.NAME, "profile-name")
email = driver.find_element(By.ID, "email")
name.send_keys("Jackson Fuller")
Thanks in advance!
You have to switch the driver to the new opened tab.
Without that the focus will remain on the first browser window.
TimeoutException actually means that Selenium could not locate element by passed locator.
Also the locator you are using is bad.
Try this:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
import time
driver = webdriver.Chrome ("C:/chromedriver.exe")
driver.get("https://10minutesemail.net/")
Copy_Email = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.ID, "copyEmailAddress"))).click()
time.sleep(10)
driver.execute_script("window.open('https://quora.com/','_blank')")
driver.switch_to.window(driver.window_handles[1])
click_Register = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.CLASS_NAME, "q-text qu-ellipsis qu-whiteSpace--nowrap"))).click()
name = driver.find_element(By.NAME, "profile-name")
email = driver.find_element(By.ID, "email")
name.send_keys("Jackson Fuller")

click on cookie popups using selenium

I am new to web scraping. For a little project i decided to try and log into instagram automatically. I am using chrome web driver to open the page and selenium to process it. i can get selenium to open Instagram but on instagram there is a cookie popup that i can't seem to automate a click for. I keep getting a error. what i want to do is just to pick the accept all.
my code is:
#Selenium imports here
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
#Other imports here
import os
#import wget
driver = webdriver.Chrome("/Users/kitchensink/Desktop/instab/chromedriver")
driver.get("https://www.instagram.com")
Accept_all = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Accept All)]"))).click()
There error that i get is:
Traceback (most recent call last):
File "/Users/kitchensink/PycharmProjects/instapost/main3.py", line 15, in <module>
Accept_all = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Accept All)]"))).click()
File "/Users/kitchensink/.local/share/virtualenvs/kitchensink-UJdjJQi/lib/python3.8/site-packages/selenium/webdriver/support/wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
See you have a issue with xpath here : (Your xpath does not contain ' at the end) you are using this
//button[contains(text(), 'Accept All)]
but one should use this instead :
//button[contains(text(), 'Accept All')]
I would do this as an xpath: /html[#class='js not-logged-in client-root js-focus-visible sDN5V']/body/div[#class='RnEpo Yx5HN _4Yzd2']/div[#class='pbNvD FrS-d gD9tr ']/div[#class='_1XyCr ']/button[#class='aOOlW bIiDR ']
So your code would look like:
#Selenium imports here
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
#Other imports here
import os
#import wget
driver = webdriver.Chrome("./chromedriver.exe")
driver.get("https://www.instagram.com")
Accept_all = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "/html[#class='js not-logged-in client-root js-focus-visible sDN5V']/body/div[#class='RnEpo Yx5HN _4Yzd2']/div[#class='pbNvD FrS-d gD9tr ']/div[#class='_1XyCr ']/button[#class='aOOlW bIiDR ']"))).click()
EDIT: I tried it and it works fine for me

web selenium testing can't locate element

This code should open flickr website and search for "s" and click search photos from suggestions.
But I get the following error:
"no such element: Unable to locate element: {"method":"xpath","selector":"//span[contains(text(),'Search photos')]"}
(Session info: chrome=90.0.4430.85)"
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
driver.get("https://www.flickr.com/")
#webdriver.support.wait.WebDriverWait(driver, 1)
driver.find_element_by_xpath("//input[#id='search-field']").send_keys("s")
webdriver.support.wait.WebDriverWait(driver, 1)
driver.find_element_by_xpath("//span[contains(text(),'Search photos')]").click()
webdriver.support.wait.WebDriverWait(driver, 1)
I think there an wrong initializing the WebDriverWait.
And to click Search photos try using .execute_script(..)method with arguments[0].click(); argument.
Try the following code snippet:
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
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
driver.get("https://www.flickr.com/")
wait = WebDriverWait(driver, 20)
search_field = wait.until(EC.element_to_be_clickable((By.XPATH, "//input[#id='search-field']")))
search_field.send_keys("s")
search_photos = wait.until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'Search photos')]")))
driver.execute_script("arguments[0].click();", search_photos)

selenium.common.exceptions.TimeoutException: this error will getting when I'm trying to button click using python

I'm using Selenium package to button click for a website. When I'm trying i'm getting an error as:
selenium.common.exceptions.TimeoutException: Message:
this is the code which trying to run.
import time
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 bs4 import BeautifulSoup as BSoup
from datetime import date, timedelta
import pyodbc
import datetime
browser = webdriver.Firefox()
browser.get("https://www.cbsl.gov.lk/rates-and-indicators/exchange-rates/daily-buy-and-sell-exchange-rates")
#time.sleep(10)
#browser.find_element_by_xpath('//*[#id="dailyexchange"]/div[2]/div/button[1]').click()
wait = WebDriverWait(browser, 20)
element = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[#id="dailyexchange"]/div[2]/div/button[1]')))
element.click()
You need to switch to iframe before clicking button:
browser.get("https://www.cbsl.gov.lk/rates-and-indicators/exchange-rates/daily-buy-and-sell-exchange-rates")
wait = WebDriverWait(browser, 20)
wait.until(EC.frame_to_be_available_and_switch_to_it('iFrameResizer2'))
element = wait.until(EC.element_to_be_clickable((By.NAME, 'select_button')))
element.location_once_scrolled_into_view
element.click()
The desired element is within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use either of the following solutions:
Using CSS_SELECTOR:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
browser = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
browser.get("https://www.cbsl.gov.lk/rates-and-indicators/exchange-rates/daily-buy-and-sell-exchange-rates")
WebDriverWait(browser, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#iFrameResizer2[src='/cbsl_custom/exratestt/exratestt.php']")))
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-default[name='select_button']"))).click()
Browser Snapshot:

Selenium + Python + onclick + NoSuchElementException

I have an "onclick" element in a webpage whose HTML reads as follows:
Text 1
I am trying to use selenium to click on this element. I tried the following code (including an implicit wait)
import time
from selenium import webdriver
driver = webdriver.Chrome()
driver.get(webpage);
driver.implicitly_wait(5)
driver.find_element_by_xpath("//a[#onclick='blah.submit()']").click()
However, it returned a NoSuchElementException
NoSuchElementException: Message: no such element: Unable to locate element:
{"method":"xpath","selector":"//a[#onclick='blah.submit()']"}
I then tried using WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get(webpage);
button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH,
"//a[#onclick='blah.submit()']")))
button.click()
However, this still didn't work and returned the following error:
TimeoutException: Message:
Does anyone know what's going wrong? Thanks in advance.

Categories