How to navigate past the popup ad using Selenium - python

I'm trying to navigate through oddschecker website using selenium in python with chrome webdriver but an add comes up. See image:
Is there a line of code I can use to get rid of the ad and access the site without having to click on the cross in top right corner manually?
Code trials:
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
mylink = 'https://www.oddschecker.com'
driver.get(mylink)
Any help would be greatly appreciated.

To navigate past the popup ad the only way is to click and close the popup ad element inducing WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
driver.get("https://www.oddschecker.com/")
# click and close the cookie banner
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[class^='CookieBannerAcceptButton']"))).click()
# click and close the popup ad
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[aria-label='Close Offers Modal'][class^='CloseButtonDesktop']"))).click()
Using XPATH:
driver.get("https://www.oddschecker.com/")
# click and close the cookie banner
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='OK']"))).click()
# click and close the popup ad
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#aria-label='Close Offers Modal' and starts-with(#class, 'CloseButtonDesktop')]"))).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

This would help you in closing the ad.
driver.get('https://www.oddschecker.com')
time.sleep(4)
driver.find_element(By.XPATH, "//*[#aria-label='Close Offers Modal']").click()
P.S. I wrote in python. If you are writing in some other language, you may transform this line per the syntax of your language.

You can integrate AdBlocker extension within you chrome driver just like this:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('load-extension=' + EXTENSION_PATH)
driver = webdriver.Chrome(DRIVER,chrome_options=chrome_options)
driver.create_options()

Related

Calendar date picker Selenium Python

Hi I am trying to figure out how to do date picking on the calendar for zacks for some personal project. unable to figure out how that works. I read a post on datepickers being used as a table and i can try that approach but i want to get date picking for future and past and on the page, only the current month shows up so would ideally like to see the onclick functionality working.
chromedriver = "/usr/bin/chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)
driver.get('https://www.zacks.com/earnings/earnings-calendar')
driver.maximize_window()
print('page load waiting ......')
time.sleep(5)
date_field = driver.find_element_by_id('earnings_calendar_events').find_element_by_id('date_select')
date_field.click() # opens up the calendar
time.sleep(2)
print('sending key 3')
date_field.send_keys('12/1/2020') #send keys doesn't work.
time.sleep(5)
To select the date 12/1/2020 within the website https://www.zacks.com/earnings/earnings-calendar 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.zacks.com/earnings/earnings-calendar')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a#date_select img"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "table.sb_minicalview td > span#dt_1"))).click()
Using XPATH:
driver.get('https://www.zacks.com/earnings/earnings-calendar')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#id='date_select']/img"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//table[#class='sb_minicalview']//td/span[#id='dt_1']"))).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:
References
You can find a couple of relevant detailed discussion in:
Select from a DatePicker in Python

No such element error while clicking on Tinder Log in button

I'm trying to learn how to interact with the internet with python, and followed a tutorial I found on how to make a bot that interacts with tinder. I am able to get a chrome window up, and it can go to the website, but I run into issues when I try to click the login button. Here is the code I used(I imported webdriver from selenium, and sleep from time, but it wouldn't transfer here):
class tinderAI():
def __init__(self):
self.driver = webdriver.Chrome()
def login(self):
self.driver.get('https://tinder.com')
sleep(2)
fb_btn = self.driver.find_element_by_xpath('//*[#id="modal-manager"]/div/div/div/div/div[3]/div[2]/button')
fb_btn.click()
The error code I get after using this code is:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="modal-manager"]/div/div/div/div/div[3]/div[2]/button"}
Any help in resolving the issue would be appreciated. Thanks!
As per your code trials the locator:
//*[#id="modal-manager"]/div/div/div/div/div[3]/div[2]/button
represents the button with text as Log in with Facebook and to invoke click() on the element you need to induce WebDriverWait for the element_to_be_clickable() reaching till the child <span> and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type = 'button'][aria-label = 'Log in with Facebook'] span"))).click()
Using XPATH:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[#type = 'button' and #aria-label = 'Log in with Facebook']//span"))).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 detailed relevant discussion in:
Selenium “selenium.common.exceptions.NoSuchElementException” when using Chrome

How to interact with the reCAPTCHA audio element using Selenium and Python

I want to, click on the button to resolve the captcha through the audio, but selenium does not detect the specified "id".
browser.get("https://www.google.com/recaptcha/api2/demo")
mainWin = browser.current_window_handle
iframe = browser.find_elements_by_tag_name("iframe")[0]
browser.switch_to_frame(iframe)
CheckBox = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID ,"recaptcha-anchor"))).click()
sleep(4)
audio = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID ,"recaptcha-audio-button"))).click()
To click() on the button to resolve the captcha through the audio as the desired 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 the following Locator Strategies:
Code Block:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get("https://www.google.com/recaptcha/api2/demo")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://www.google.com/recaptcha/api2/anchor']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span#recaptcha-anchor"))).click()
driver.switch_to.default_content()
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='recaptcha challenge']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#recaptcha-audio-button"))).click()
Browser Snapshot:
Reference
Ways to deal with #document under iframe
Outro
You can find a couple of relevant discussions in:
How to click on the reCaptcha using Selenium and Java
CSS selector for reCaptcha checkbok using Selenium and vba excel
Find the reCAPTCHA element and click on it — Python + Selenium
Very useful, just put your attention, that text: 'recaptcha challenge' in selector below depends from regional settings/language:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='recaptcha challenge']")))

How to click on the Read the guide button using Selenium after logged in through Python

I need a script which logs me in to github and presses a Read the guide button. I've got it to log in but after it's done it does not work any more. Please help.
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('https://github.com/session')
login_area = browser.find_element_by_name('login')
login_area.send_keys('maximmashkov')
login_area = browser.find_element_by_id('password')
login_area.send_keys('12345')
submit_button = browser.find_element_by_name('commit')
submit_button.click()
To click() on the element with text as Read the guide you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following solutions:
Using LINK_TEXT:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Read the guide"))).click()
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn.btn-primary[href='https://guides.github.com/activities/hello-world/']"))).click()
Using XPATH:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#href='https://guides.github.com/activities/hello-world/' and text()='Read the guide']"))).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
Most probably your script fails to wait for the "Read the guide" button to appear, consider using Explicit Wait so Selenium could wait for a defined period of time for the element to appear in DOM
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
#initialize your browser here
login_area = browser.find_element_by_name('login')
login_area.send_keys('maximmashkov')
login_area = browser.find_element_by_id('password')
login_area.send_keys('12345')
submit_button = browser.find_element_by_name('commit')
submit_button.click()
submit_button = browser.find_element_by_name('commit')
submit_button.click()
read_the_guide = element = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.LINK_TEXT, "Read the guide")))
read_the_guide.click()
browser.quit()
More information:
How to use Selenium to test web applications using AJAX technology
How to wait for elements in Python Selenium WebDriver
The solution was posted in the question and moved here:
You only have to put a little time.sleep() delay. One second is enough

How to click on the popup element in chrome using Selenium and Python

During test my automation code, I met a horrible web page.
When I click a element, the page present new browser window and alert.
After that, I can't do anything because the alert is invincible.
How can I go through this?
My environment is as following:
Python 3.6.7
Selenium 3.141.0
Please try this.
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.kebhana.com/foreign/index.do')
el = driver.find_element_by_xpath('//*[#id="header"]/div[2]/div/div[2]/div/div/ul/li[21]/ul/li[2]/ul/li[4]/a')
driver.execute_script("arguments[0].click();",el)
Then, you can see a new browser window with alert.
And I can't find any solution to dismiss that alert.
If you have some brilliant way to handle alert, please show me.
Here is the logic to switch to the new window and then accept the alert.
# this will switch to the new window
driver.switch_to.window(driver.window_handles[-1])
# now accept the alert
driver.switch_to.alert.accept()
The element is a Layer Message and part of the HTML DOM and to locate and to click/dismiss the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following solutions:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a#opbLayerMessage0_OK[href$='HanaBank']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#id='opbLayerMessage0_OK' and contains(#href, 'HanaBank')]"))).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

Categories