Selenium Webdriver Python-多个标签中的批量操作

阿里斯

我做了一个自动化项目,在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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Python Selenium Webdriver同时处理多个Windows

来自分类Dev

Python Selenium Webdriver:调试

来自分类Dev

StaleElementReferenceException Selenium WebDriver Python

来自分类Dev

Selenium Webdriver Python storeTextPresent

来自分类Dev

Selenium Webdriver中的IF语句

来自分类Dev

Selenium Python一次处理多个Webdriver

来自分类Dev

网站在多个页面上识别Python Selenium Webdriver

来自分类Dev

Selenium WebDriver Python,搜索WebElement

来自分类Dev

Python Selenium:处理Webdriver异常

来自分类Dev

Selenium Webdriver 获取元素(Python)

来自分类Dev

Python Selenium webdriver超时异常

来自分类Dev

单击Selenium WebDriver和Python中的JavaScript链接

来自分类Dev

专注于Selenium Webdriver和Python中的新窗口

来自分类Dev

使用Python中的Selenium Webdriver获取td类的标题

来自分类Dev

Python Selenium:是否可以在虚拟环境中安装WebDriver

来自分类Dev

如何在Python中从selenium.webdriver获取HTML?

来自分类Dev

使用Python中的Selenium Webdriver获取td类的标题

来自分类Dev

在参数中传递Selenium Webdriver。Python可能吗?

来自分类Dev

我想在python的Ubuntu 16.04系统中安装Selenium Webdriver

来自分类Dev

Selenium WebDriver:上载多个文件

来自分类Dev

在Selenium WebDriver中禁用元素

来自分类Dev

Selenium webdriver 中的缺货验证

来自分类Dev

Selenium Webdriver Java 中的 JavascriptExecutor

来自分类Dev

使用Python和Selenium Webdriver抓取JavaScript

来自分类Dev

使用Selenium Webdriver(Python)循环链接

来自分类Dev

Python Selenium Webdriver-尝试除循环

来自分类Dev

存储图像的标题文本Selenium Webdriver Python

来自分类Dev

使用Selenium WebDriver作为基类python

来自分类Dev

无法在Python中将Firefox WebDriver与Selenium连接

Related 相关文章

热门标签

归档