Closing popup window by clicking close button in selenium - python

I want to close the popup window that appears when I hit a particular url. Here is the "inspect element" window of that page:
Here is what I have tried:
driver.find_element_by_css_selector("i[#class='popupCloseIcon']").click()
But it gives following error:
InvalidSelectorException: Message: Given css selector expression
"i[#class='popupCloseIcon']" is invalid: InvalidSelectorError:
Document.querySelector: 'i[#class='popupCloseIcon']' is not a valid
selector: "i[#class='popupCloseIcon']"
Here is the url where popup appears: https://www.investing.com/equities/oil---gas-dev-historical-data
Once the url is opened through selenium, the pop up appears after a few seconds.
How can I click that close button?

The popup appears after some time, so you need wait to solve this. And you have invalid selector : i[#class='popupCloseIcon'], please use i[class*='largeBannerCloser']
Try the below:
driver.get('https://www.investing.com/equities/oil---gas-dev-historical-data')
try:
popup = WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "i[class*='largeBannerCloser']")))
popup.click()
except TimeoutException as to:
print(to)
This is wait until 60 seconds maximum.
Following import:
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.common.exceptions import TimeoutException

As the pop up appears after a few seconds accessing the url https://www.investing.com/equities/oil---gas-dev-historical-data to to close the popup window you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 60).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "i.popupCloseIcon.largeBannerCloser"))).click()
Using XPATH:
WebDriverWait(driver, 60).until(EC.element_to_be_clickable((By.XPATH, "//i[#class='popupCloseIcon largeBannerCloser']"))).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

First, "i[#class='popupCloseIcon']" is a invalid css selector locator, it should be "i[class='popupCloseIcon']". Second, there are four elements mapped to the "i[class='popupCloseIcon']", the css selector "div.right>i.popupCloseIcon" will help you to locate the target element

Related

How to click special button with selenium in python?

I am trying to use selenium to click certain buttons within the bank of america simulator, but the buttons don't seem to ever click. No new link is reached, which is something I haven't encountered before.
https://message.bankofamerica.com/onlinebanking_demo/OLB_Simulator/
I want to click "Sign in options" and then click "Sign in: Recognized device"
I tried using selenium to click the button and I get no error. Nothing happens at all and the program continues, so I know it's not an issue with not finding the button. My current code is as follows:
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get('https://message.bankofamerica.com/onlinebanking_demo/OLB_Simulator/')
sleep(3)
login_button = driver.find_element("id", "landing_sign")
driver.execute_script("arguments[0].click();", login_button);
This code worked fine for me.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get('https://message.bankofamerica.com/onlinebanking_demo/OLB_Simulator/')
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.ID, "landing_sign")).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[aria-labelledby='signInOpt3']")).click()
NOTE: Using sleep is a bad practice, use WebDriverWait and wait for the specific state you need instead.
To click on the clickable elements you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following locator strategies:
Code block:
driver.get('https://message.bankofamerica.com/onlinebanking_demo/OLB_Simulator/')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#landing_sign"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href='/onlinebanking_demo/OLB_Simulator/SignIn/recognized']"))).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
Browser Snapshot:

Clicking a button with Selenium button

I'm trying to click on "Agree" button on this website https://www.soccerstats.com/matches.asp?matchday=1# but it didn't work for me using this code:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time
s=Service("C:/Users/dhias/OneDrive/Bureau/stgg/chromedriver.exe")
driver=webdriver.Chrome(service=s)
driver.get("https://www.soccerstats.com/matches.asp?matchday=1#")
driver.maximize_window()
time.sleep(1)
driver.find_element(By.CLASS_NAME," css-47sehv").click()
the css-47sehv is the class name of the button and here is a picture of the button The blue button
Though the element AGREE contains the classname css-47sehv, the value looks dynamic and may change in a short interval or once the application gets restarted.
Solution
To click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
driver.get("https://www.soccerstats.com/matches.asp?matchday=1#")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[mode='primary']"))).click()
Using XPATH:
driver.get("https://www.soccerstats.com/matches.asp?matchday=1#")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#mode='primary' and text()='AGREE']"))).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
You have to make sure of following:
1-use explicitly Wait to wait the button to/Till appear
try:
element=WebDriverWait(driver,10).until(
EC.presence_of_element_located((By.ID, "AgreeButton"))
)
finally:
driver.quit()
2-Click on the button with correct Xpath:
driver.find_element(By.XPATH,"//button[text()='AGREE']").click()
3-If simple click doesn't work you can use JavaScript and perform methods to click.
Try using this
driver.find_element_by_class_name('css-47sehv').click()
on place of
driver.find_element(By.CLASS_NAME," css-47sehv").click()
To click on AGREE button use the following xpath to identify the element and click.
//button[text()='AGREE']
Code:
driver.find_element(By.XPATH,"//button[text()='AGREE']").click()
Or Use following css selector.
driver.find_element(By.CSS_SELECTOR,"button.css-47sehv").click()

Python selenium I can't acess to a linked page on a page with iframe

I want to go from this page to this https://resultats.ffbb.com/organisation/b5e6211d5970.html to this page https://resultats.ffbb.com/championnat/b5e6211f621a.html?r=200000002810394&d=200000002911791&p=2 by clicking on 'Régional féminin U15'.
I have tried many solutions but the best I had, is not working systematically.
Please coul you help me?
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
import selenium.webdriver.support.ui as ui
import time
driver = webdriver.Firefox()
driver.get("https://resultats.ffbb.com/organisation/b5e6211d5970.html")
driver.switch_to.frame("idIframeChampionnat")
#sign_in = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '/html/body/pre/span[93]'))).click();
button = driver.find_element_by_partial_link_text(u"minin U15")
button.click()```
To click() on the link with text as Régional féminin U15 as the elements are 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 Locator Strategies:
Using CSS_SELECTOR:
driver.get("https://resultats.ffbb.com/organisation/b5e6211d5970.html")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#idIframeChampionnat")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Régional féminin U15"))).click()
Using XPATH:
driver.get("https://resultats.ffbb.com/organisation/b5e6211d5970.html")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='idIframeChampionnat']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Régional féminin U15"))).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
Browser Snapshot:
Reference
You can find a couple of relevant discussions in:
Switch to an iframe through Selenium and python
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
selenium in python : NoSuchElementException: Message: no such element: Unable to locate element
Try
driver.switch_to.frame("idIframeChampionnat")
button = driver.find_element_by_xpath("//a[contains(text(), 'minin U15')]")
button.click()
or
driver.switch_to.frame("idIframeChampionnat")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(text(), 'minin U15')]"))).click()
This is my code example in groovy (sorry, haven't python env), pretty similar to yours, and it works perfectly 20 times in a loop even without timeouts.
#Test(invocationCount = 20)
void test() {
driver.get('https://resultats.ffbb.com/organisation/b5e6211d5970.html')
driver.switchTo().frame(
driver.findElement(By.xpath("//iframe[#id='idIframeChampionnat']"))
)
driver.findElement(By.xpath("//a[contains(text(), 'minin U15')]")).click()
driver.switchTo().defaultContent()
assert driver.getCurrentUrl() ==
'https://resultats.ffbb.com/championnat/b5e6211f621a.html?r=200000002810394&d=200000002911791&p=2'
}
So, I have no ideas. Maybe just add driver.switch_to.default_content() after click?

"Accept Cookies" popup with Selenium in Python using explicit wait

I have been trying to webscrape some information from a webside with Selenium in Python. I load the webside, but I can't accept the cookies, which is necessary to continue. I have tried to use explicitly wait as I suspected that the problem was that the webside loaded before the "accept" bottom becomes clickable.
My code is:
import os
import until as until
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
os.environ['PATH'] += r"C:\Users\BackUp HDL\AppData\Local\Programs"
driver = webdriver.Chrome()
driver.get("https://watchmedier.dk/latest/filtered? sitesFilter=policywatch.dk&sitesFilter=shippingwatch.dk&sitesFilter=mobilitywatch.dk&sitesFilter=energiwatch.dk&sitesFilter=finanswatch.dk&sitesFilter=ejendomswatch.dk&sitesFilter=mediawatch.dk&sitesFilter=agriwatch.dk&sitesFilter=fodevarewatch.dk&sitesFilter=medwatch.dk&sitesFilter=kapwatch.dk&sitesFilter=itwatch.dk&sitesFilter=ctwatch.dk&sitesFilter=watchmedier.dk&sitesFilter=advokatwatch.dk")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="notice"]/div[3]/button[2]')))
driver.find_element_by_xpath('//*[#id="notice"]/div[3]/button[2]').click()
Whenever I try to run the code I get an error. Can someone please help me out?
The element Accepter 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 Locator Strategies:
Using CSS_SELECTOR:
driver.get("https://watchmedier.dk/latest/filtered? sitesFilter=policywatch.dk&sitesFilter=shippingwatch.dk&sitesFilter=mobilitywatch.dk&sitesFilter=energiwatch.dk&sitesFilter=finanswatch.dk&sitesFilter=ejendomswatch.dk&sitesFilter=mediawatch.dk&sitesFilter=agriwatch.dk&sitesFilter=fodevarewatch.dk&sitesFilter=medwatch.dk&sitesFilter=kapwatch.dk&sitesFilter=itwatch.dk&sitesFilter=ctwatch.dk&sitesFilter=watchmedier.dk&sitesFilter=advokatwatch.dk")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='SP Consent Message']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[title='Accepter']"))).click()
Using XPATH:
driver.get("https://watchmedier.dk/latest/filtered? sitesFilter=policywatch.dk&sitesFilter=shippingwatch.dk&sitesFilter=mobilitywatch.dk&sitesFilter=energiwatch.dk&sitesFilter=finanswatch.dk&sitesFilter=ejendomswatch.dk&sitesFilter=mediawatch.dk&sitesFilter=agriwatch.dk&sitesFilter=fodevarewatch.dk&sitesFilter=medwatch.dk&sitesFilter=kapwatch.dk&sitesFilter=itwatch.dk&sitesFilter=ctwatch.dk&sitesFilter=watchmedier.dk&sitesFilter=advokatwatch.dk")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#title='SP Consent Message']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#title='Accepter']"))).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
Browser Snapshot:
Reference
You can find a couple of relevant discussions in:
Ways to deal with #document under iframe
Switch to an iframe through Selenium and python
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
selenium in python : NoSuchElementException: Message: no such element: Unable to locate element

How could I select from RadioButtons using selenium in python?

I tried this code to select a radio button:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.asiamiles.com/en/enrolment.html')
gender = driver.find_element_by_id("gender_Female")
gender.click()
I received this error
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
Where could the problem be, and how can I solve it?
To click() on the radio button for female you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get("https://www.asiamiles.com/en/enrolment.html")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"label[for='gender_Female']"))).click()
Using XPATH:
driver.get("https://www.asiamiles.com/en/enrolment.html")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[#for='gender_Female']"))).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
Browser Snapshot:
Reason for getting ElementNotInteractableException is that the radio button coordinates are x=0 and y=0, so selenium is not able to click.
Please use this below approach.
imports needed:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Script:
url = 'https://www.asiamiles.com/en/enrolment.html'
driver.get(url)
female = WebDriverWait(driver,30).until(EC.presence_of_element_located((By.CSS_SELECTOR,'.radio-label.bodytext4.F')))
female.location_once_scrolled_into_view
female.click()
only using id it wont work when you hover on label tag it shows shaded region of the radio button that's where you need to click. for addition to that it takes a while to load a page so for smooth action use action class and it will click on element.
actions = ActionChains(driver)
gender = driver.find_element_by_xpath(".//input[#name='gender']/following-sibling::label[#for='gender_Female']")
actions.move_to_element(gender).perform()

Categories