I'd like to close this window opened up on Amazon web site by using Selenium with Python. I've tried find_element_by_xpath, but it doesn't work. Here's the snippet of the code;
close_to_list = browser.get("/html/body/div[4]/div/div/div[2]/div[2]/div[2]/div[1]/div[2]/div/div/table/tbody/tr[2]")
I get the xpath of that 'X' button but I guess I need to close it as switch_to_alert, but I'm new to this era so I couldn't write it properly.
Here's the image view;
enter image description here
You don't need to use switch_to_alert() as well as get() to close modal window. Just try to close it with below code:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button[aria-label="Close"]'))).click()
This allows you to wait for button appearance and click the button
Related
At first I opened the link through driver.get and pressed on a button through execute_script but when I try to press using execute_script on another button on the new tab which the first button opened it gives that exception.
I want to open tradingview open btc and click on indicators.
My code:
from selenium import webdriver
from selenium.webdriver import Chrome, ChromeOptions
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
from selenium.webdriver.common.action_chains import ActionChains
import time
driver = webdriver.Chrome("E:\Chrome driver\chromedriver.exe")
driver.get("https://www.tradingview.com/symbols/BTCUSDTPERP/ideas/?exchange=BINANCE")
advanced = driver.find_element(By.CSS_SELECTOR,"#anchor-page-1 > div > div.tv-category-header__buttons.tv-category-header__buttons--adaptive.tv-category-header__buttons--hide-if_bigger-than_phone > div > a.btn-Phr0VjiT.btnDesktop-Phr0VjiT.button-OvB35Th_.size-large-OvB35Th_.color-brand-OvB35Th_.variant-secondary-OvB35Th_.with-start-icon-OvB35Th_.with-end-icon-OvB35Th_")
driver.execute_script("arguments[0].click()",advanced)
time.sleep(20)
chart = driver.find_element(By.CSS_SELECTOR,"#header-toolbar-chart-styles > div")
driver.execute_script("argument[0].click()",chart)
I tried to press button after the first button opened a new tab called indicators but gives that exception I am trying to open tradingview and open btc after that click on indicators.
When a new tab is opened and you want to do something with selenium, first you have to switch to that tab:
driver.switch_to.window( driver.window_handles[-1] )
The [-1] means that you are switching to the last opened tab.
So put this command right after time.sleep(20)
I am trying to close a pop up window with selenium in python, which is not allowing my code to execute further. But I am unable to do that. There is a a pop up window which asks me if i want to sign up but it keeps popping up at inconsistent times. Is there a method to check wether or not the pop-up window is active?
My code so far:
import selenium
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
import time
PATH = "C:\Program Files (x86)\chromedriver.exe";
driver = webdriver.Chrome(PATH);
driver.get("https://www.investing.com/news/")
time.sleep(3)
accept_cookies = driver.find_element_by_xpath('//*[#id="onetrust-accept-btn-handler"]');
accept_cookies.click();
So your problem is that you don't want the signup popup to display.
I was just poking the site's script, I found a really nice way for you to work around it: set user agent to gene. The script to display popup will be disabled if user agent matches some mobile browsers.
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
opts = Options()
opts.add_argument("user-agent=gene")
driver = webdriver.Chrome(options=opts)
# Adds the cookie into current browser context
driver.get("https://www.investing.com/news/")
time.sleep(60) # wait for a minute to see if the popup happens
driver.quit()
Run this code, observe the page, it will not display the signup popup.
Alternative work around:
Use a Chrome profile that already turned off the Signup popup, will also not display the popup
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Path") #Path to your chrome profile
If you really need to close the popup without these methods, define a check function with try/except. Before doing any actions, call this function to check if there's popup, then close the popup, save the state to some variable (so you don't check it anymore). Since the function has try/except, it will not raise Exception and your code will run.
You can do this with 2 quick methods that I can think of immediately.
1:
You will use this often
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
then when you want to get an element that needs time to load and you want to do actions with it, you implement it with WebDriverWait :
wait = WebDriverWait(driver, 10)
try:
accept_cookies = wait.until(EC.presence_of_element_located((By.XPATH, "'//*
[#id=\"onetrust-accept-btn-handler\"]'")))
catch:
# probably you have a bad locator and the element doesn't exist or it needs more than 10 sec to load.
else:
# logic here is processed after successfully obtaining the pop up
accept_cookies.click()
I would recommend using ActionChains
from selenium.webdriver.common.action_chains import ActionChains
then after you obtain the pop up element make the click like this:
actions = ActionChains(driver)
actions.move_to_element(accept_cookies).click().perform()
This is the equivalent of moving the mouse pointer to the middle of the pop up close btn and clicking it
I am Trying to create a bot that can fill a cart with these beer bottles. I really want to do this on a few different sites but for some reason i can only get it to open the page and click the first button, then it doesn't click the next button. I tried ID, Name, pretty much anyway to identify the button but it won't click it. I even tried sleep for 3 seconds. I tried to see if it was in an Iframe but i don't think it is. I'm out of ideas.... Link is https://www.sideprojectbrewing.com/shop?category=Beer+Release
I'm trying to access the add to cart element but does not seem to work
\
from Config import keys
from selenium import webdriver
def order(k):
driver = webdriver.Chrome(executable_path=r"C:\Users\ezliv\Desktop\ShopBot1\chromedriver_win32\chromedriver.exe")
driver.get(k['product_url'])
driver.find_element_by_xpath('//*[#id="thumb-biereblanche"]/div/div[1]/div/div/img').click()
driver.find_element_by_xpath('//*[#id="yui_3_17_2_1_1606181545139_755"]').click()
\\
You can try following code:
driver.get(url_here)
wait = WebDriverWait(driver, 20)
bottle = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[#id="thumb-biereblanche"]/div/div[1]/div/div/img')))
bottle.click()
add_to_cart = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'sqs-add-to-cart-button-inner')))
add_to_cart.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
I want to click the "OK" button in this pop up dialog
I tried:
driver.switchTo().alert().accept();
but it doesn't work
To click on the OK button within the alert you need to induce WebDriverWait for the desired alert_is_present() and you can use the following solution:
WebDriverWait(driver, 10).until(EC.alert_is_present())
driver.switch_to.alert.accept()
Note : You have to add the following imports :
from selenium.webdriver.common.alert import Alert
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Reference
You can find a couple of relevant discussions in:
Python click button on alert
How to read the text from the alert box using Python + Selenium
Why switching to alert through selenium is not stable?
Would like to understand why switch_to_alert() is receiving a strikethrough and how to fix
After handling a pop up overlay the rest of the code no longer works.
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('https://www.vapordna.com/login.asp')
#handling popup
browser.find_element_by_css_selector("div.age-verify-button.age-modal__btn.btn.btn-primary").click()
browser.find_element_by_link_text("Yes").click()
If I don't close the pop up the rest of the code works, if I close the popup none of the code after that point works.
emailElem = browser.find_element_by_name("email")
emailElem.send_keys("****#****.com")
passwordElem = browser.find_element_by_name("password")
passwordElem.send_keys("******")
passwordElem.submit()
I have tried adding this line but it doesn't help,
browser.switchTo().defaultContent();
To my understanding the popup isn't in a new iframe or window so I'm not sure why there would be any problem with continuing to interact with the page.
I also tried clicking on the body before continuing
browser.find_element_by_tag_name("body").click()
After seeing your website I observe this is neither an frame nor a window, I just a form so there is no need to use here browser.switchTo().defaultContent(); which actually using for switching from any frame to default, Actually when you are going to find this button on the opened popup which overlay all the page, It would not be full loaded on the page, so you should try using WebDriverWait to wait until this button visible on the page.
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
browser = webdriver.Firefox()
browser.get('https://www.vapordna.com/login.asp')
wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.age-verify-button.age-modal__btn.btn.btn-primary"))).click()
Now after clicking on this agreement button you can proceed further steps for login.