Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

how to rotate multiple cookies with selenium python

$
0
0

`hello please i am new to selenium, i need help in writing a code that can rotate proxy, rotate user agent and and rotate cookies and then click on ads but it is giving me hard time can anyone help please, the bot currently randomly select the links from list of links.csv but does not get to the cookies and user agent

from selenium import webdriverfrom selenium.webdriver.common.action_chains import ActionChainsfrom selenium.webdriver.common.keys import Keysimport timeimport randomimport csvimport json# Load the user agents from useragent.csvuseragents = []with open('useragent.csv', 'r') as file:    reader = csv.reader(file)    for row in reader:        useragents.append(row[0])# Load the cookies from cookie.jsonwith open('cookie.json', 'r') as file:    cookies = json.load(file)# Create a new instance of the Chrome driverdriver = webdriver.Chrome()# Maximize the browser windowdriver.maximize_window()# Set a random user agentuser_agent = random.choice(useragents)options = webdriver.ChromeOptions()options.add_argument(f'user-agent={user_agent}')driver = webdriver.Chrome(options=options)# Visit google.comdriver.get('https://google.com')# Set the cookiesfor cookie in cookies:    driver.add_cookie(cookie)# Send random keys from links.csvwith open('links.csv', 'r') as file:    reader = csv.reader(file)    links = list(reader)random_link = random.choice(links)search_input = driver.find_element_by_name("q")search_input.send_keys(random_link[0])search_input.send_keys(Keys.RETURN)# Click on the first Google resultfirst_result = driver.find_element_by_css_selector('div#search div.g:nth-child(1) a')first_result.click()# Mouse hover to the first postfirst_post = driver.find_element_by_css_selector('div.post:nth-child(1)')hover = ActionChains(driver).move_to_element(first_post)hover.perform()# Scroll down slowlydriver.execute_script("window.scrollTo(0, document.body.scrollHeight/2);")time.sleep(3)driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")time.sleep(3)# Click on the first postfirst_post.click()# Wait for 5 secondstime.sleep(5)# Go back one pagedriver.back()# Mouse hover over to the second postsecond_post = driver.find_element_by_css_selector('div.post:nth-child(2)')hover = ActionChains(driver).move_to_element(second_post)hover.perform()# Scroll down slowlydriver.execute_script("window.scrollTo(0, document.body.scrollHeight/2);")time.sleep(3)driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")time.sleep(3)# Click on the second postsecond_post.click()# Wait for 5 secondstime.sleep(5)# Click on Related Articles or related postrelated_articles = driver.find_element_by_css_selector('div.related-articles')related_articles.click()# Wait until popup appears and click on the popuppopup = driver.find_element_by_css_selector('div.popup')if popup.is_displayed():    popup.click()else:    pass# Repeat the process for 5 timesfor _ in range(5):    # Perform the actions again    # ...    pass# Stop the processdriver.quit()

Viewing all articles
Browse latest Browse all 23131

Trending Articles