Selenium webdriver python - 多个选项卡中的批量操作

Selenium webdriver python - Bulk action in multiple tabs

我在 selenium webdriver 上做了一个自动化项目 chrome python 该代码运行良好但仅在一次(即在一个选项卡中) 我想做的是在几个选项卡上执行此操作(同时打开大约 50 个选项卡) 这是我想要的场景: 1/ 打开浏览器然后连接 2/ 它在单个浏览器中打开所有 50 个 URL(即 50 个选项卡) 3/ 它返回到第 1 个选项卡执行操作,完成后,它转到第 2 个选项卡,依此类推,直到完成所有 50 个选项卡。

我的代码分为 2 个,我分别 运行 它们并且效果很好,但我试图合并它们但没有用。

第一个代码:添加信息,然后在操作结束时单击发布按钮

from webdriver_manager.chrome import ChromeDriverManager
import time
import os
import urllib3
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains



driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
driver.delete_all_cookies()
driver.get("https://business.google.com/posts/l/02451881054225948987")


driver.find_element_by_id("identifierId").send_keys("*******@gmail.com")
time.sleep(3)

driver.find_element_by_xpath('//*[@id="identifierNext"]/div/button/div[2]').click()
time.sleep(3)

driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[2]/div/div[2]/div/div/div[2]/div/div[1]/div/form/span/section/div/div/div[1]/div[1]/div/div/div/div/div[1]/div/div[1]/input').send_keys("************")
time.sleep(3)

driver.find_element_by_xpath('//*[@id="passwordNext"]/div/button/div[2]').click()
time.sleep(3)



driver.find_element_by_xpath('//*[@id="yDmH0d"]/c-wiz/div[2]/div[1]/c-wiz/div[1]/div/div/span/div/div[9]/div/span/span/div').click()
time.sleep(3)

#titre posts
driver.find_element_by_xpath('//*[@id="c30"]').send_keys("Lorem ipsum")
time.sleep(2)

# supprimer le contenu dans le field
driver.find_element_by_xpath('//*[@id="c33"]').clear()
time.sleep(2)

# date debut
driver.find_element_by_xpath('//*[@id="c33"]').send_keys("1 déc. 2020")
time.sleep(2)

#ESC 1st JS Window
driver.find_element_by_xpath('//*[@id="c33"]').send_keys(Keys.ESCAPE)
time.sleep(2)

# end date
driver.find_element_by_xpath('//*[@id="c40"]').send_keys("31 déc. 2020")
time.sleep(2)

#ESC 2nd JS Window
driver.find_element_by_xpath('//*[@id="c40"]').send_keys(Keys.ESCAPE)
time.sleep(2)

#expand extra field
driver.find_element_by_xpath('/html/body/div[7]/div[4]/div/div[2]/span/div/div/div/section/div[2]/div/div[1]/div[2]/div[6]/button/span').click()
time.sleep(2)

#add description
driver.find_element_by_xpath('/html/body/div[7]/div[4]/div/div[2]/span/div/div/div/section/div[2]/div/div[1]/div[2]/div[7]/div/div/label/span[2]/textarea').send_keys("Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,")
time.sleep(2)

#select CTA button
driver.find_element_by_xpath('/html/body/div[7]/div[4]/div/div[2]/span/div/div/div/section/div[2]/div/div[1]/div[2]/div[8]/div[2]/div/div[1]/button/div').click()
time.sleep(2)

#select buy button
driver.find_element_by_xpath('/html/body/div[7]/div[4]/div/div[2]/span/div/div/div/section/div[2]/div/div[1]/div[2]/div[8]/div[2]/div/div[2]/div/ul/li[4]/span[2]').click()
time.sleep(2)

#add CTA URL
driver.find_element_by_xpath('/html/body/div[7]/div[4]/div/div[2]/span/div/div/div/section/div[2]/div/div[1]/div[2]/div[8]/div[3]/label/input').send_keys("https://www.example.fr")
time.sleep(2)

#upload photo
driver.find_element_by_xpath('/html/body/div[7]/div[4]/div/div[2]/span/div/div/div/section/div[2]/div/div[1]/div[2]/div[2]/div[1]/div/div[1]/div/span').click()
time.sleep(2)

#step 2 upload photo
driver.find_element_by_xpath('//*[@id=":f"]/div').send_keys("/Users/*******/Documents/2020/Clients FR/*******.png")
time.sleep(2)



#clic publish
driver.find_element_by_xpath('/html/body/div[7]/div[4]/div/div[2]/span/div/div/div/section/div[2]/div/div[2]/div/div[2]/div/button/div[2]').click()
time.sleep(2)

#2nd code: a code that allows you to open several tabs at the same time.


from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
import time


driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
driver.delete_all_cookies()
urls = ["https://business.google.com/posts/l/02451881054225948987",
        "https://business.google.com/posts/l/02451881054225948923",
        "https://business.google.com/posts/l/02451881054225948988",
        "https://business.google.com/posts/l/02451881054225948909",
        "https://business.google.com/posts/l/02451881054225948943",
        "https://business.google.com/posts/l/02451881054225948922"]
time.sleep(1)
for posts in range(len(urls)):
    #print(posts)
    driver.get(urls[posts])
    if(posts!=len(urls)-1):
        driver.execute_script("window.open('');")
        chwd = driver.window_handles
        driver.switch_to.window(chwd[-1])

#Can someone help me to make my code work?
Thanks

嗯,Selenium 被认为不是线程安全的。所以,我不确定你会得到什么结果。事实上告诉你这是如何为你工作的。将出现 window/tab 处于焦点的问题。这是您可以尝试的方法。尝试使用 3-4 个选项卡,看看会发生什么。

from multiprocessing import Pool

from selenium import webdriver

def check_ip(url):
    driver = webdriver.Chrome(executable_path='C:/chromedriver.exe')
    driver.get(url)
    #Do something

if __name__ == '__main__':
    with open('urls.txt') as f: #Put the urls in a text with each url on a new line.No commas.
        urls = f.read().splitlines()

    with Pool() as pool:
        pool.map(check_ip, urls)