我做了一个自动化项目,在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被认为不是线程安全的。因此,我不确定您会得到什么结果。实际上,告诉它如何为您工作。窗口/选项卡将成为焦点。这是您可以尝试的方法。尝试说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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句