I am using Selenium with Python on a webpage with JavaScript. The script runs until a pop=up asks for a click either to agree or seek More Options labels appear. The HTML for the pop-up disappears if an option is manually clicked. Would appreciate some guidance on how to click "Agree" automatically.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(executable_path='C:/A38/chromedriver_win32/chromedriver.exe')
driver.implicitly_wait(30)
driver.maximize_window()
# Navigate to the application home page
driver.get("https://www.sportinglife.com/racing/results/2020-11-23")
To click on the button with text AGREE. First induce waits for page load and then wait for the element to be clickable.
driver.get('https://www.sportinglife.com/racing/results/2020-11-23')
wait=WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='AGREE']"))).click()
Import
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
This will do the trick:
driver.find_element_by_xpath('//*[#id="qc-cmp2-ui"]/div[2]/div/button[2]').click()
Related
I recently started learning Selenium and webscraping in Python. I'm trying to find and click the 'Accept All' button on the pop-up (image of the pop-up can be found below) when entering the following site: https://www.sherdog.com, using Chrome. It takes around 5 seconds for the pop-up to load. I have tried different things and have red what I could find on stackoverflow describing similar problems. To no avail. I always get the NoSuchElementException (or NoAlertPresentException).
I have tried the following things:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver.get('https://www.sherdog.com')
driver.find_element(By.CLASS_NAME, 'Button__StyledButton-a1qza5-0 incZp')
driver.switch_to.alert
try:
element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, 'Button__StyledButton-a1qza5-0 incZp')))
except:
print("An exception occurred")
I also thought I might have to switch frame using driver.switchTo().frame(driver.findElement(By.id("rufous-sandbox"))), but am honestly unsure which frame to select. When looking through the HTML code (which I just started learning) I see some references to JavaScript (of which I have zero knowledge). Maybe that is causing me trouble?
If anybody could provide some insight, or point me in the right direction, would be greatly appreciated.
This is how you click that element:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
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.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('disable-notifications')
chrome_options.add_argument("window-size=1920,1080")
webdriver_service = Service("chromedriver/chromedriver") ## path to where you saved chromedriver binary
browser = webdriver.Chrome(service=webdriver_service, options=chrome_options)
actions = ActionChains(browser)
url = 'https://www.sherdog.com'
browser.get(url)
elem = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#title='Scroll to the bottom of the text below to enable this button']")))
elem.click()
print('clicked')
Bear in mind that, if window is not sufficiently large, that text will need to be scrolled (and button will not be clickable). Default headless window size is quite small, so make sure your window is sufficiently large.
Selenium docs: https://www.selenium.dev/documentation/
To click on the element Accept Cookies 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.sherdog.com")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div>a.cnaccept"))).click()
Using XPATH:
driver.get("https://www.sherdog.com")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='cnaccept']"))).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
There are 2 objects you need to close there and they are not alerts. So driver.switch_to.alert is not relevant here.
Always try using stable unique locators. Class names like incZp may often be dynamic and not reliable.
Button__StyledButton-a1qza5-0 incZp are actually 2 class names, so you have to use CSS_SELECTOR or XPATH to work with them.
It is always preferred to wait for element visibility, not just existence when you going to click on that element.
This should work:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver.get('https://www.sherdog.com')
wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.XPATH, "//button[contains(text(),'Continue')]"))).click()
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div#cookieNotice a.cnaccept"))).click()
I am able to add single and all available items, but not sure how to add multiple items
enter image description here
enter image description here
Your question isn't clear, please the next time provide more details and please just don't share the code with a screenshot.....
Anyway,
To click all the buttons:
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()
driver.get("https://rahulshettyacademy.com/seleniumPractise/#/")
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, "[class='product-action']")))
for i in driver.find_elements(by=By.CSS_SELECTOR, value="[class='product-action']"):
i.click()
To click just the first two buttons (again, not sure what you really want):
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()
driver.get("https://rahulshettyacademy.com/seleniumPractise/#/")
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, "[class='product-action']")))
for i in driver.find_elements(by=By.CSS_SELECTOR, value="[class='product-action']")[0:2]:
i.click()
Remove unnecessary element from items using methods items.remove(), items.pop(), del items[].
I want to access an website with selenium and than a addblock-window appears, in which i need to click a button for it to disappear.
Eventhough I can find my XPath(//button[#title='Einverstanden'], /html/body/div/div[2]/div[3]/div[1]/button[#title = 'Einverstanden'] or
//button[contains(text(),"Einverstanden")]') in the browser,
I can't find it with my Python script. And i can't seem to find the mistake.
Here is my code.
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.Firefox()
driver.implicitly_wait(30)
driver.get("``https://www.derstandard.at/story/2000134260361/endspiel-vor-gericht-prozess-gegen-boris-becker-startet-in-london``")
driver.maximize_window()
x = driver.find_element(By.XPATH, "//button[#title = 'Einverstanden']")
print(x)
This is the error I'm getting.
The button is enclosed in an iframe in which case, you first need to switch to the iframe and then access the element
This should work:
driver.get("https://www.derstandard.at/consent/tcf/")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//div[contains(#id, 'sp_message_container')]//iframe")))
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button[title='Einverstanden']"))).click()
Wait Imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
In case you want to switch to default frame again, you may use this when required:
driver.switch_to.default_content()
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get('https://www.worldometers.info/coronavirus/country/canada/')
time.sleep(1)
button = driver.execute_script("window.scrollTo(0, 5500)")
button1 = driver.find_element_by_xpath('/html/body/div[4]/div[2]/div[1]/div[8]/div/div[9]/a/button')
button1.click()
Trying to click this button using xpath but it just doesn't seem to click the button to extend the webpage. I first tried to copy by xpath which didn't work and then I copied the full xpath which also didn't work. After that, I tried to find by name and enter the text on the button and click the button. But none of these methods actually worked and clicked the button. So how can I click this button so that it extends the webpage allowing me to scrape more of the webpage?
The problem was that the button wasn't at "clickable state" despite was rendered in page at moment.
To obviate this, you have to add an explicit wait to tell the driver to wait until it's state change to clickable.
Try to use this code instead :
from selenium import webdriver
import time
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('https://www.worldometers.info/coronavirus/country/canada/')
time.sleep(1)
button = driver.execute_script("window.scrollTo(0, 5500)")
button1 = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[4]/div[2]/div[1]/div[8]/div/div[9]/a/button')))
button1.click()
To click on View more News Induce WebdriverWait and wait for presence_of_element_located() and following css selector.
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
driver = webdriver.Chrome()
driver.get('https://www.worldometers.info/coronavirus/country/canada/')
button=WebDriverWait(driver,20).until(EC.presence_of_element_located((By.CSS_SELECTOR,"a.load-more__btn>.btn")))
button.location_once_scrolled_into_view
button.click()
Or Use following xpath
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
driver = webdriver.Chrome()
driver.get('https://www.worldometers.info/coronavirus/country/canada/')
button=WebDriverWait(driver,20).until(EC.presence_of_element_located((By.XPATH,"//a[#class='load-more__btn']/button[#class='btn']")))
button.location_once_scrolled_into_view
button.click()
https://www.sevenonemedia.de/tv/programm/programmwochen
Here I want to login:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
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
options = Options()
chrome_path = "T:/Markus/WebScrapingExample/Chromedriver/chromedriver.exe"
driver = webdriver.Chrome(executable_path=chrome_path,chrome_options=options)
driver.set_window_size(1280, 720)
time.sleep(5)
driver.get("https://www.sevenonemedia.de/tv/programm/programmwochen")
driver.find_element_by_id("_58_login").send_keys("name")
driver.find_element_by_id("_58_password").send_keys("pw")
driver.find_element_by_xpath('//*[#id="sign-in-button"]').click()
ElementNotInteractableException: element not interactable
(Session info: chrome=78.0.3904.97)
This is my error
Id is there. Why does this happen?
This page contains duplicate of element with ID sign-in-button. If your selector points to more than one element, driver always takes the first from the top of the DOM one which is not interactable in this case. You must refer to the second element with this id. Try this selector for "Sign in" button:
//*[#id="aheadcustom_p_p_id_58"]//button
hi first things you should not give your password to all of the stackoverflow community :)
you can't click on the button because there is a popup at the bottom of the page and you have to click on it first for selenium it's hidding your button
last is that the totality of your code ?
if yes you forgot
driver = webdriver.Firefox() #or any other webdriver
you have not create driver without this line
EDIT !!
it wasn't working with only the modification above but with this one its good
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
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
time.sleep(5)
driver = webdriver.Firefox()
driver.get("https://www.sevenonemedia.de/tv/programm/programmwochen")
driver.find_element_by_id("_58_login").send_keys("login")
driver.find_element_by_id("_58_password").send_keys("pssd")
driver.find_element_by_xpath("/html/body/div[1]/div/div/div[2]/a").click()
driver.find_element_by_css_selector("#_58_fm > fieldset:nth-child(1) > div:nth-child(6) > button:nth-child(1)").click()
this work :)
sometime css selector a safer and work better
To click on the Login button you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following solutions:
Using XPATH:
driver.get("https://www.sevenonemedia.de/tv/programm/programmwochen")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#title='Anmelden' and not(contains(#name,'INSTANCE'))]"))).send_keys("a.mai#mediaplus.com")
driver.find_element_by_xpath("//input[#title='Passwort' and not(contains(#name,'INSTANCE'))]").send_keys("Edidaten17")
driver.find_element_by_xpath("//input[#title='Passwort' and not(contains(#name,'INSTANCE'))]//following::button[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:
The problem is that your button locator isn't unique on the page. It finds two buttons, the first of which is not visible which causes the ElementNotInteractableException.
The simple fix is to use the CSS selector below
#main-content #sign-in-button
That will find only the button you want. So your last few lines of code would be
driver.find_element_by_id("_58_login").send_keys("name")
driver.find_element_by_id("_58_password").send_keys("pw")
driver.find_element_by_css_selector('#main-content #sign-in-button').click()
sleep(1)
login_box = driver.find_element_by_name('login')
login_box.click()
this is for facebook login button, just inspect the website and see the id/name/type to make your code automate to work properly