Close popups in Oddschecker using Selenium Python - python

Need to close the cookies and ad popup on this page, I know you can do it using webdriverwait and either CSS_SELECTOR or X tags but I can't find the specific tags for the buttons, as when you click 'inspect' they disappear.
Code trials:
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
from selenium.webdriver.chrome.service import Service
s=Service('C:\Program Files (x86)\chromedriver.exe')
driver = webdriver.Chrome(service=s)
driver.get(https://www.oddschecker.com/)
Any help greatly appreciated.

To click on the element OK 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.oddschecker.com/")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[class^='CookieBannerAcceptButton']"))).click()
Using XPATH:
driver.get("https://www.oddschecker.com/")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='OK']"))).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

Related

Selenium click()

"""
Web scraping the wikipidia page
"""
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://en.wikipedia.org/wiki/Main_Page")
num_articles1 = driver.find_element(By.CSS_SELECTOR, '#articlecount a')
print(num_articles1.text)
num_articles1.click()
driver.close()
Question: num_articles1 returns the value but why is the click() not working ?
can't understand why is this happening, what am i missing?
On my Windows 10 using latest Selenium, ChromeDriver and Chrome the number of articles i.e. 6,559,615 gets printed perfectly and the click() is also performed perfecto.
However, to click on the clickable element ideally 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, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#articlecount a"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#id='articlecount']/a"))).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

element not interactable: [object SVGGElement] has no size and location error interacting with search field on YouTube homepage using Selenium Python

Code trials:
from selenium import webdriver
from selenium.webdriver import Keys, ActionChains
from selenium.webdriver.chrome.service import Service
from time import time
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
service = Service(executable_path="C:\\Users\\aps\\Desktop\\Python\\chromedriver.exe")
driver = webdriver.Chrome(service=service)
driver.get("https://youtube.com")
search = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "search")))
ActionChains(driver).move_to_element(search).pause(2).click_and_hold().send_keys("Iktarfa").perform()
button = driver.find_element(By.ID, "search-icon-legacy").click()
Search field is getting fetched.
But after that I am getting following error, I am out of ideas and a new learner. Please HELP!!
The locator strategy which you have used:
(By.ID, "search")
identifies multiple elements within the HTML DOM.
and the first WebElement having the property style="display: none;". Hence you see the error.
Solution
To send a character sequence to 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:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "ytd-searchbox#search"))).send_keys("text")
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ytd-searchbox[#id='search']"))).send_keys("text")
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
There is no need to add a Actions class for this scenario. It is perfectly capable of doing the search after just using the click and then searching for the element.
I used this code piece and it works perfectly fine
import time
from seleniumwire import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
svc = Service(ChromeDriverManager().install())
options = Options()
options.add_argument('--allow-running-insecure-content')
options.add_argument('--ignore-certificate-errors')
driver = webdriver.Chrome(service=svc,options=options)
driver.maximize_window()
driver.get("https://youtube.com")
search = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, "search")))
driver.find_element(By.NAME,'search_query').click()
time.sleep(5)
driver.find_element(By.NAME,'search_query').send_keys('Iktarfa')
button = driver.find_element(By.ID, "search-icon-legacy").click()
time.sleep(5)
driver.quit()
I've used the sleep method, but you should in general use an intelligent wait mechanism like Explicit waits.
Output screenshot -

How can I use selenium to click a button

HTML snapshot of website:
The button is the rebounds button on 'https://app.prizepicks.com/board'. I've tried using the copy xPath feature, but that does not work.
Try to wait until the elements show up or use time.sleep()
Below xpath works for me
rebounds_btn = driver.find_element_by_xpath('//*[contains(text(), "Rebounds")]')
rebounds_btn.click()
Try the below,
WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.XPATH, "//*[contains(text(), 'Rebounds')]"))).click()
do not forget to import
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
To click on the element with text as Rebounds you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following solution:
driver.get("https://app.prizepicks.com/board")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='Rebounds']"))).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

Trying to click a button but keep getting an error with xpath

I am trying to automate pulling credit charges into an excel sheet; I have managed to get the login working. Once I enter the website, there's a button titled "Search". I cant seem to figure out how to have that button clicked.
from selenium import webdriver
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 TimeoutException
from selenium.webdriver.common.by import By
import time
chromedriver = "C:/Python_Ex/chromedriver_win32/chromedriver.exe"
driver = webdriver.Chrome(chromedriver)
delay = 30
driver.get("https://global.americanexpress.com/activity/date-range?from=2020-05-01&to=2020-05-30")
driver.find_element_by_xpath('//*[#id="eliloUserID"]').send_keys("removed")
driver.find_element_by_xpath('//*[#id="eliloPassword"]').send_keys("removed")
driver.find_element_by_xpath('//*[#id="loginSubmit"]').click()
time.sleep(10)
#print(driver.find_elements_by_xpath('//*[#id="root"]/div[1]/div/div[2]/div/div/div[5]/div/div[3]/div/div/div/div[2]/div[2]/div/div/div/div/div[1]/section/div[4]/div[2]/button'))
search_button = driver.find_elements_by_xpath('//*[#id="root"]/div[1]/div/div[2]/div/div/div[5]/div/div[3]/div/div/div/div[2]/div[2]/div/div/div/div/div[1]/section/div[4]/div[2]/button')
search_button.click()
html tag is as follows
<button class="btn btn-fluid" tabindex="0" type="button"> <span>Search</span></button>
Xpath as follows
//*[#id="root"]/div[1]/div/div[2]/div/div/div[5]/div/div[3]/div/div/div/div[2]/div[2]/div/div/div/div/div[1]/section/div[4]/div[2]/button
Any help is appreciated.
To click() on the <button> with text as Search using Selenium 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, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-fluid[type='button']>span"))).click()
Using XPATH:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn btn-fluid']/span[text()='Search']"))).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
After login submited, try to add the below code:
...
...
driver.find_element_by_xpath('//*[#id="loginSubmit"]').click()
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn btn-fluid']//span[text()='Search']"))).click()
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

Selenium Can't find element when using find_element through Python

Keep getting unable to locate element error message.
The first find element by xpath was fine, but the second one is giving me a hard time.
Here is my code:
import XLUtils
from selenium import webdriver
driver=webdriver.Chrome(executable_path="C:\Chrome_Driver\Chromedriver.exe")
driver.get("https://www.canada.ca/en/revenue-agency/services/e-services/e-services-businesses/payroll-deductions-online-calculator.html")
driver.find_element_by_xpath('/html/body/main/div[1]/div[7]/p/a[1]').click()
driver.find_element_by_xpath('//*[#id="welcome_button_next"]').click()
You'll need to wait a bit for the Next button to appear.
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
wait = WebDriverWait(driver, 10)
e = wait.until(
EC.presence_of_element_located((By.XPATH, '//*[#id="welcome_button_next"]'))
)
e.click()
To click on the element with text as Next you need 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, "input.btn.btn-primary#welcome_button_next"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='btn btn-primary' and #id="welcome_button_next"]"))).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