简体   繁体   中英

python selenium, get over javascript popup

I'm working on a project and I have to go to a site and drop a soy there. I am trying to do this using the Python selenium module, but when I enter the site with a bot, I get a popup (an acceptance form about cookies). I cannot achieve what I am trying to do without pressing accept.

在此处输入图像描述

I checked the network section of the site and found the site containing cookies, when I enter that site the code works properly and it succeeds in pushing the accept cookies button, but this does not work for me because it cannot even find the accept button on the main site, I know it is not because it is written in javascript, but i don't know how to do this.

Anyway, let's get to the code part.

on the site I'm trying to login

the site that sent the cookie form the site uses

this code works for this :

from selenium import webdriver
import time
from bs4 import BeautifulSoup
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

option = webdriver.ChromeOptions()
option.add_argument('--disable-notifications')
option.add_argument("--mute-audio")

driver = webdriver.Chrome("chromedriver.exe", options=option)


driver.get('https://consent-pref.trustarc.com/?type=jefftest_ibm&site=ibm.com&action=notice&country=tr&locale=tr&behavior=expressed&gtm=1&layout=default_eu&irm=undefined&from=https://consent.trustarc.com/#')


time.sleep(10)
driver.find_elements_by_class_name("call")[0].click()

but it doesn't work for the other, how can I get this to work for the other too?

You could use pyautogui instead to stimulate the click:

import pyautogui

pyautogui.click(x=100, y=200)

@The Pilot Dude 's answer works in a way, but it can't be said to be very dynamic so:

I went to the site with original chrome and accepted cookies and saved these cookies. When I had to re-enter the site later, I reloaded the cookies.

for download cookies:

import pickle
import time
from selenium import webdriver

option = webdriver.ChromeOptions()
option.add_argument("--disable-notifications")
option.add_argument("--mute-audio")
option.add_argument("user-data-dir=C:\\Users\\___*******__\\AppData\Local\\Google\\Chrome\\User Data")
driver = webdriver.Chrome(options = option)

driver.get('https://speech-to-text-demo.ng.bluemix.net/')

time.sleep(10)#in this time I accepted cookies

pickle.dump(driver.get_cookies(), open("cookies.pkl","wb"))#for download cookies

and after that I can use cookies for chromedriver.exe

for use cookies:

import pickle
from selenium import webdriver

option = webdriver.ChromeOptions()
option.add_argument("--disable-notifications")
option.add_argument("--mute-audio")
driver = webdriver.Chrome("chromedriver.exe", options = option)

driver.get('https://speech-to-text-demo.ng.bluemix.net/')

file = open("cookies.pkl","rb")
cookies = pickle.load(file)
for cookie in cookies:
    driver.add_cookie(cookie)#for use cookies
file.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM