How to Accept Cookies alert with Selenium in Python - python

I'm trying to get to google.com and type something in the search bar. But the cookies alert pop-up always gets in my way. So I have to click the button 'I agree'. I know I have to wait a little bit of time before searching for the element but even if I'll wait with WebDriverWait() function, or .implicitly_wait() it just doesn't want to find the element (I used the search by xpath and .click() to press the button).
Spent hours trying to find a solution...
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
def main():
driver = webdriver.Chrome('chromedriver.exe')
url = 'https://www.google.com/'
driver.get(url)
# The following line is supposed to wait for the button to appear and then click:
agreeButton = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//*[#id='introAgreeButton']")))
agreeButton.click()
main()
Also i should include the error here:
Traceback (most recent call last):
File "{SCRIPT PATH}", line 49, in <module>
main()
File "{SCRIPT PATH}", line 46, in main
agreeButton = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//*[#id='introAgreeButton']")))
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:

Related

Python > Getting stuck on Proceed to Checkout

New to Python.
Creating smoke test for Checkout process.
Am unable to click the Proceed to Checkout button.
On the Add-Ons page of the Checkout process, I am unable to click the Proceed to Checkout button which allows the user to go to the Attendee Info page of Checkout.
I've tried a few different things. Below is the most basic attempt.
Any guidance would be appreciated.
import time
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(ChromeDriverManager().install())
username = 'bmendenhall#sans.org'
password = 'Lloydcole2!'
wait = WebDriverWait(driver, 10)
driver.get('https://www.sans.org/')
driver.find_element(By.CSS_SELECTOR, "a[aria-label='Log In']").click()
driver.find_element(By.CSS_SELECTOR, "input[id='username']").send_keys(username)
driver.find_element(By.CSS_SELECTOR, "input[id='password']").send_keys(password)
driver.find_element(By.CSS_SELECTOR, "input[id='regularsubmit']").click()
time.sleep(3)
driver.get('https://registration.sans.org/sans/s/cart/0a62J000000ClOsQAK')
time.sleep(3)
driver.find_element(By.CSS_SELECTOR, "button[title='proceed to checkout']").click()
time.sleep(10)
driver.close()
Error Messaging:
Traceback (most recent call last): File "/Users/BMendenhall/Desktop/Projects/sans-domain-testing/AddtoCartcontinue.py", line 28, in <module>
driver.find_element(By.CSS_SELECTOR, "button[title='proceed to checkout']").click() File "/Library/Python/3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 857, in find_element
return self.execute(Command.FIND_ELEMENT, { File "/Library/Python/3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 435, in execute
self.error_handler.check_response(response) File "/Library/Python/3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"button[title='proceed to checkout']"} (Session
I don't know why but CSS_SELECTOR has problem even with button[title] but XPATH works for me
find_element(By.XPATH, "//button[#title='proceed to checkout']")
BTW:
first I tested it with find_elements (with s at the end) and it finds two buttons with 'proceed to checkout' - maybe this makes some problem with CSS.

Unable to click radio button even after using explicit wait on selenium

I am trying to select 'Female' Radio Button in the webpage
import time
import selenium.common.exceptions
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Chrome(executable_path="C:\Drivers\chrome\chromedriver.exe")
driver.get("https://fs2.formsite.com/meherpavan/form2/index.html?1537702596407")
wait = WebDriverWait(driver, 60)
element = wait.until(EC.element_to_be_clickable((By.ID, "RESULT_RadioButton-7_1")))
driver.execute_script("arguments[0].click();",element)
#element.click()
#driver.find_element_by_id("RESULT_RadioButton-7_1").click()
print(driver.find_element_by_id("RESULT_RadioButton-7_0").is_selected())
print(driver.find_element_by_id("RESULT_RadioButton-7_1").is_selected())
Error:
C:\Users\kkumaraguru\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/kkumaraguru/PycharmProjects/SeleniumProject/RadioButtons.py
Traceback (most recent call last):
File "C:\Users\kkumaraguru\PycharmProjects\SeleniumProject\RadioButtons.py", line 14, in <module>
element = wait.until(EC.element_to_be_clickable((By.ID, "RESULT_RadioButton-7_1")))
File "C:\Users\kkumaraguru\PycharmProjects\pythonProject\venv\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Process finished with exit code 1
It seems that it times out waiting for an element with ID RESULT_RadioButton-7_1 to be present on the page. I'd open the page yourself to make sure such element is present. You can do this using javascript in the browser's console: document.getElementById("RESULT_RadioButton-7_1"). If this doesn't work then try to debug through the code, and check what HTML Selenium is looking at to make sure is what you expect.
You can do that using JS intervention, Also make sure to maximize the windows size like below :
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
driver.implicitly_wait(30)
driver.get("https://fs2.formsite.com/meherpavan/form2/index.html?1537702596407")
#time.sleep(5)
element = driver.find_element(By.ID, "RESULT_RadioButton-7_1")
driver.execute_script("arguments[0].click();", element)

Python Selenium - Identifying and Clicking Angular Elements

Utilizing Python and Selenium, I am attempting to click on the 'Matrix' and 'Transaction Desk' Angular buttons on a specific webpage (see screenshot). I have looked into this for hours now and have not found anything that has worked. It actually seems like there are a lot of unsolved questions still out there around this topic. The source code (see screenshot) does not appear to be within any iframe. The traceback just tells me that it could not find the specific code and times out.
My code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
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("C:\\Users\\Matt\\Documents\\WebDriver\\chromedriver_win32\\chromedriver.exe")
driver.get("https://www.stellarmls.com/")
#Password authentication HERE, so you will be unable to access this site yourself.
driver.maximize_window()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="collapse911"]/app-links-panel-body/div/div/div[1]'))).click()
print(driver.page_source)
Traceback:
Traceback (most recent call last):
File "C:\Users\Matt\Documents\Splitt\ROI Property Finder.py", line 53, in <module>
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="collapse911"]/app-links-panel-body/div/div/div[1]'))).click()
File "C:\Users\Matt\Python3.9\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Supplementary screenshots:
iframe:

Selenium can't find id element in <div>

I'm trying to get an HTML text off a webpage using python selenium. But Selenium appears to be unable to locate the element. Not sure if I am doing anything wrong and is looking for some answers here.
Please see a part of my code below:
wait(driver, 10).until(EC.visibility_of_element_located((By.ID, 'current_address_name')))
full_address = driver.find_element_by_id('current_adress_name')
street_address = full_address.text.split('(S)')[0]
print('Street Address: ' + str(street_address))
all_street_address.append(street_address)
This is the error message (timeout). Supposedly because it cannot find the element:
Exception in thread Thread-1:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/threading.py", line 932, in _bootstrap_inner
self.run()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "/Volumes/GoogleDrive/My Drive/PycharmProjects/googlestuff/gsheet_test_mod.py", line 121, in web_extraction
wait(driver, 10).until(EC.visibility_of_element_located((By.ID, 'current_address_name')))
File "/Users/cadellteng/googstuff/lib/python3.8/site-packages/selenium/webdriver/support/wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
And this is a part of the HTML code:
<div id="current_address_name" style="display:none">4 Third Avenue (S)266576</div>
A little background about this thing though. This part of the element doesn't seem to be a visible element on the page because when I put my mouse over the element, it does not highlight any part of the page. Is it because Selenium cannot extract elements that are not displayed? Or am I doing something wrong here? Please advise.
Just in case you need the full code, this is the page: Singapore Streetdirectory
The visibility_of_element_located method is for "checking that an element is present on the DOM of a page and visible.".
Clearly this element is not visible, as the element has style="display:none" set.
You could use presence_of_element_located instead, which will just check that the element is present in the DOM tree:
wait(driver, 10).until(EC.presence_of_element_located((By.ID, 'current_address_name')))
Fixing a few other issues with your code, the full minimal, reproducible example would be:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium import webdriver
all_street_address = []
driver = webdriver.Chrome()
wait = WebDriverWait
driver.get("https://www.streetdirectory.com/sg/4-third-avenue-266576/1_27869.html")
wait(driver, 10).until(EC.presence_of_element_located((By.ID, 'current_address_name')))
full_address = driver.find_element_by_id('current_address_name')
street_address = full_address.get_attribute('innerHTML').split('(S)')[0]
print('Street Address: ' + str(street_address))
all_street_address.append(street_address)

Can't perform a click on a search button because of some ad hiding it

I've written a script in python with selenium to perform a search in a webpage using search-box available there. However, When I run my script, It throws an error which I'm gonna paste below. The thing is when the webpage is loaded through my script, there is an advertisement pops up hiding the search-box. How can I get around that and fetch the search result? Thanks in advance.
Link to that site: webpage
Script I'm trying with:
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()
wait = WebDriverWait(driver, 10)
driver.get("replace_with_above_site")
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "input#q"))).send_keys("Camden Medical Centre, 1 Orchard Boulevard 248649")
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "input#search_button"))).click() ##error thrown here
driver.quit()
Traceback I'm having:
Traceback (most recent call last):
File "C:\Users\WCS\AppData\Local\Programs\Python\Python36-32\demo.py", line 12, in <module>
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "input#search_button"))).click()
File "C:\Users\WCS\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Users\WCS\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 628, in _execute
return self._parent.execute(command, params)
File "C:\Users\WCS\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute
self.error_handler.check_response(response)
File "C:\Users\WCS\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Element <input type="button" id="search_button" onclick="submitSearch();"> is not clickable at point (868, 137). Other element would receive the click: <div id="splash_screen_overlay"></div>
This is the ad which hides the search box:
Btw, the search parameter is available within .send_keys() in my script. Anything from the populated result will suffice.
The simplest solution is to simulate exactly the same action user should do: close an ad in case it appeared:
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
from selenium.common.exceptions import NoSuchElementException
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)
driver.get("replace_with_above_site")
try:
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn_close"))).click()
except NoSuchElementException:
pass
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "input#q"))).send_keys("Camden Medical Centre, 1 Orchard Boulevard 248649")
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "input#search_button"))).click()
driver.quit()
Use below code. This will click the element even if it is hidden beneath ad
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].scrollIntoView(true)", driver.GetElement(By.Id("someID")));

Categories

Resources