I am currently working on a project where I am automating whatsapp messages through url. like this https://wa.me/number_here
whenever i do it normally everything goes fine, but when I try to automate this a whatsapp popup box appears and blocks everything, I mean everything, no right-click no developer options, that is why i cant get the x path of the button on that popup. i have tried (driver.shift_to,alert.close)but it says that there is no such alert. i have tried to find the button by contains(text) method but also did not work. here is my code.
chrome_options = Options()
chrome_options.add_argument('--user-data-dir = user-data')
chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])
chrome_options.add_argument('--disable-notifications')
chrome_options.add_argument('--disable-popup-blocking')
chrome_options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=chrome_options, executable_path= driver_path)
wait = WebDriverWait(driver, 60)
contact = f'https://wa.me/{number}'
driver.get(contact)
time.sleep(3)
alert = wait.until(EC.alert_is_present())
alert.accept()
please help me how to bypass this popup. thanks
I think, this link can be helpful for you:
https://stackoverflow.com/a/19019311/12000849
What I do is to set a conditional delay with WebDriverWait just before the point I expect to see the alert, then switch to it, like this:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
browser = webdriver.Firefox()
browser.get("url")
browser.find_element_by_id("add_button").click()
try:
WebDriverWait(browser, 3).until(EC.alert_is_present(),
'Timed out waiting for PA creation ' +
'confirmation popup to appear.')
alert = browser.switch_to.alert
alert.accept()
print("alert accepted")
except TimeoutException:
print("no alert")
Related
I am trying to press the accept button in a cookies popup in the website https://www.immobilienscout24.de/
Snapshot:
I understand that this requires
driver.execute_script("""return document.querySelector('#usercentrics-root')""")
But I can't trickle down the path to the accept button in order to click it. Can anyone provide some help?
This is one way (tested & working) you can click that button: please observe the imports, as well as the code after defining the browser/driver:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
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
chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('disable-notifications')
import time as t
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.immobilienscout24.at/regional/wien/wien/wohnung-kaufen'
browser.get(url)
page_title = WebDriverWait(browser, 3).until(EC.presence_of_element_located((By.CSS_SELECTOR, "a[title='Zur Homepage']")))
actions.move_to_element(page_title).perform()
parent_div = WebDriverWait(browser, 20000).until(EC.presence_of_element_located((By.ID, "usercentrics-root")))
shadowRoot = browser.execute_script("return arguments[0].shadowRoot", parent_div)
try:
button = WebDriverWait(shadowRoot, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[data-testid='uc-accept-all-button']")))
button.click()
print('clicked')
except Exception as e:
print(e)
print('no click button')
That page is reacting to user's behavior, and it will only fully load the page once it detects mouse movements, hence the ActionChains() part of the code. After that, we drill down into the shadow root element, we locate the button (using Waits, to make sure it's clickable), and then we click it.
Selenium documentation can be found at https://www.selenium.dev/documentation/
The element Alle akzeptieren within the website is located within a #shadow-root (open).
Solution
To click on the element Alle akzeptieren you have to use shadowRoot.querySelector() and you can use the following Locator Strategy:
Code Block:
driver.execute("get", {'url': 'https://www.immobilienscout24.de/'})
time.sleep(10)
item = driver.execute_script('''return document.querySelector('div#usercentrics-root').shadowRoot.querySelector('button[data-testid="uc-accept-all-button"]')''')
item.click()
I need to accept cookies on a specific website but I keep getting the NoSuchElementException. This is the code for entering the website:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
chrome_options = Options()
driver = webdriver.Chrome(executable_path='./chromedriver', options=chrome_options)
page_url = 'https://www.boerse.de/historische-kurse/Erdgaspreis/XD0002745517'
driver.get(page_url)
time.sleep(10)
I tried accepting the cookie button using the following:
driver.find_element_by_class_name('message-component message-button no-children focusable button global-font sp_choice_type_11 last-focusable-el').click()
driver.find_element_by_xpath('//*[#id="notice"]').click()
driver.find_element_by_xpath('/html/body/div/div[2]/div[4]/div/button').click()
I got the xpaths from copying the xpath and the full xpath from the element while using google chrome.
I am a beginner when it comes to selenium, just wanted to use it for a short workaround. Would appreciate some help.
The button Zustimmen is in iframe so first you'd have to switch to the respective iframe and then you can interact with that button.
Code:
driver.maximize_window()
page_url = 'https://www.boerse.de/historische-kurse/Erdgaspreis/XD0002745517'
driver.get(page_url)
wait = WebDriverWait(driver, 30)
try:
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[starts-with(#id,'sp_message_iframe')]")))
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Zustimmen']"))).click()
print('Clicked successfully')
except:
print('Could not click')
pass
Imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
I've got a similar problem with a page. I've tried to address the iframe and the "accept all" button with selenium (as suggested by #cruisebandey above).
However, the pop-up on this page seems to work differently:
https://www.kreiszeitung-wochenblatt.de/hanstedt/c-panorama/mega-faslams-umzug-in-hanstedt_a270327
This is what I've tried:
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(executable_path="C:\\Users\\***\\chromedriver.exe")
driver.maximize_window()
try:
driver.get("https://www.kreiszeitung-wochenblatt.de/hanstedt/c-panorama/mega-faslams-umzug-in-hanstedt_a270327")
except:
print('Site not found')
wait = WebDriverWait(driver,10)
try:
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,'/html/body/iframe')))
except:
print('paywall-layover not found')
try:
cookie = wait.until(EC.element_to_be_clickable((By.XPATH,'//*[#id="consentDialog"]/div[3]/div/div[2]/div/div[2]/div/div[1]/div[2]/div')))
cookie.click()
except:
print('Button to accept all not found')
I'm trying to write Login credentials using send_keys element method with Selenium on this alert:
Login Alert
I get this alert after clicking on a button in a new window but I can't inspect the elements on this window.
here is my code
import time
import pynput
from selenium import webdriver
driver=webdriver.Chrome("C:/Users/dhias/OneDrive/Bureau/stgg/chromedriver.exe")
driver.get("http://10.15.44.177/control/userimage.html")
time.sleep(3)
window_before = driver.window_handles[0]
driver.maximize_window()
btn=driver.find_element_by_name("TheFormButton3")
btn.click()
window_after = driver.window_handles[1]
driver.switch_to.window(window_after)
obj = driver.switch_to.alert
time.sleep(2)
obj.send_keys('Admin')
time.sleep(2)
I get this error that tells me that there is no alert
I can't acess to the page so I can't try my code but, first I optimized It by adding WebdriverWait and the new By. function:
from selenium import webdriver as wd
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = wd.Chrome("C:/Users/dhias/OneDrive/Bureau/stgg/chromedriver.exe")
wait = WebDriverWait(driver, 15)
driver.get("http://10.15.44.177/control/userimage.html")
driver.maximize_window()
window_before = driver.window_handles[0]
wait.until(EC.element_to_be_clickable((By.NAME, "TheFormButton3"))).click()
driver.switch_to.window(driver.window_handles[1])
alert = driver.switch_to.alert
alert.send_keys('Admin\npassword\n') # \n works as an ENTER key
You can try this code to see if the alert really exists or is an element of the webpage:
from selenium.common.exceptions import NoSuchElementException
def is_alert_present(self):
try:
self.driver.switch_to_alert().send_keys('Admin\npassword\n') # \n works as an ENTER key
print('Sucess')
except NoSuchElementException:
print('No Alert Present')
Also see Alerts in the Selenium Documentation
Also Try:
parent_h = browser.current_window_handle
# click on the link that opens a new window
handles = browser.window_handles # before the pop-up window closes
handles.remove(parent_h)
browser.switch_to_window(handles.pop())
# do stuff in the popup
# popup window closes
browser.switch_to_window(parent_h)
# and you're back
So finally I've found a solution..I used the approach where you have to send username and password in URL Request and it worked:
http://username:password#the-site.com
I'm trying to install chrome extention with selenium but when I clicked on "Add Extension", there was an alert like the picture below showed up, I'm not sure if it is an alert or not , but I saw error "no such alert". please help me
this is how I tried to handle this
alert = driver.switch_to.alert
alert.accept()
P/s : I also tried some other ways of handling alert but it wasn't worked so I don't think it is an alert. And I also don't want to use add_extension option
EDITED
Well, if you want to click on the alert message, try using this
WebDriverWait(driver, wait_time).until(EC.alert_is_present())
alert_msg = driver.switch_to.alert
alert_msg.accept()
For some reason I was only able to click on the alert when I used the WebDriverWait to wait until the alert is present.
Workaround
If you want to add a extension you should use the options argument.
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_extension('path/to/extension.crx')
driver = webdriver.Chrome('path/to/chromedriver.exe', options=options)
To download the CRX file use this extension: link
workaround number 2
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, wait
from time import sleep
import pyautogui
driver = webdriver.Chrome(r'.\drivers\chromedriver.exe')
driver.get('https://chrome.google.com/webstore/detail/phantom/bfnaelmomeimhlpmgjnjophhpkkoljpa')
elem = WebDriverWait(driver, 5).until(
EC.element_to_be_clickable((By.XPATH, '/html/body/div[3]/div[2]/div/div/div[2]/div[2]/div/div')))
elem.click()
sleep(3)
pyautogui.hotkey('tab','enter', interval=0.1)
I'm trying to use auto login in to a site using selenium through pyhton but it throws
http: 405 error "Pardon our Interruption. something about your browser made us think you were a bot"
What can I do to avoid it? I would like to see the execution live to check if the code is working correctly which I can't do if I use it in headless mode. Am I wrong?
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
usernameStr = 'bucmi1#yandex.com'
passwordStr = 'pmz4'
browser = webdriver.Chrome()
browser.get(('https://www.milanuncios.com/mis-anuncios/'))
# fill in username and strike a subsequent button
username = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.ID, 'email')))
username.send_keys(usernameStr)
# wait for transition then continue to fill items
password = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.ID, 'contra')))
password.send_keys(passwordStr)
signInButton = browser.find_element_by_class_name('submit btnSend')
signInButton.click()
Thanks in advance.
It is not clear why you got an error message as :
http: 405 error "Pardon our Interruption. something about your browser made us think you were a bot"
But I was able to successfully able to login through the credentials you provided with your own code adding some minor tweaks through chrome.options in incognito mode as follows :
Code Block :
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
usernameStr = 'bucmi1#yandex.com'
passwordStr = 'pmz4'
options = Options()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
options.add_argument('--incognito')
browser = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
browser.get(('https://www.milanuncios.com/mis-anuncios/'))
# fill in username and strike a subsequent button
username = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, 'email')))
username.send_keys(usernameStr)
# wait for transition then continue to fill items
password = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, 'contra')))
password.send_keys(passwordStr)
signInButton = browser.find_element_by_css_selector("div.btnEnviarFrm>input.submit.btnSend[value^='INICIAR']")
signInButton.click()
Browser Snapshot :
Thanks for the answers! The problem had something to do with my default profile folder. Don't know exactly what since there are many files I don't understand. But once I created a new one using options.add_argument("user-data-dir=C:\\dir\\of\\example\\profile")
could access without problems.