英文:
Why is Firefox selenium webdriver not processing more then 20 tabs?
问题
我正在使用以下脚本将我的PNG图像上传到一个秘密网站:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import os
def select_image_for_tab(driver, image_file):
# 查找并点击“浏览文件”按钮
browse_button = driver.find_element(By.XPATH, "//button[text()='Browse for a file']")
browse_button.click()
# 等待文件输入框可见并可交互
time.sleep(0.5)
# 使用Selenium处理文件上传对话框
file_input = driver.find_element(By.XPATH, "//input[@type='file']")
file_input.send_keys(image_file)
# 等待文件上传完成并关闭文件选择对话框
WebDriverWait(driver, 20).until(EC.invisibility_of_element_located((By.XPATH, "//input[@type='file']")))
if __name__ == "__main__":
image_files_directory = r"E:\Desktop\social\Output_folder\folder 20"
# 提示关注Firefox
print("请关注Firefox。脚本将在5秒后开始...")
time.sleep(5)
# 设置Firefox驱动
driver = webdriver.Firefox()
try:
# 在新标签页中打开网站
driver.get("UPLOAD_URL.com")
# 等待网站加载和目标元素可点击
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "(//div[@class='ms-Stack css-147']//div)[1]")))
# 点击目标元素
target_element = driver.find_element(By.XPATH, "(//div[@class='ms-Stack css-147']//div)[1]")
target_element.click()
# 获取目录中所有PNG图像文件的列表
image_files = [f for f in os.listdir(image_files_directory) if f.lower().endswith('.png')]
# 为每个标签页选择图像文件
for image_file in image_files:
select_image_for_tab(driver, os.path.join(image_files_directory, image_file))
time.sleep(0.5) # 如果需要,调整此延迟
# 打开新标签页并加载网站
driver.execute_script("window.open('about:blank');")
driver.switch_to.window(driver.window_handles[-1])
driver.get("UPLOAD_URL.com")
# 等待新标签页中的网站加载和目标元素可点击
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "(//div[@class='ms-Stack css-147']//div)[1]")))
time.sleep(4) # 如果需要,添加额外等待时间
print("已为所有标签页完成文件选择。")
except Exception as e:
print("发生错误:", e)
finally:
# 脚本完成后不会关闭浏览器或任何标签页
pass
但是,每次我都需要将文件分成20个一组。Firefox不能处理超过20个标签页。如何在远程Firefox中绕过这个限制?
注意:请不要询问秘密URL或UPLOAD_URL.com
英文:
I am using the following script to upload my png images to a secret website:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import os
def select_image_for_tab(driver, image_file):
# Find and click on "Browse for a file" button
browse_button = driver.find_element(By.XPATH, "//button[text()='Browse for a file']")
browse_button.click()
# Wait for the file input to be visible and interactable
time.sleep(0.5)
# Handle the file upload dialog using Selenium
file_input = driver.find_element(By.XPATH, "//input[@type='file']")
file_input.send_keys(image_file)
# Wait for the file to be uploaded and the file selection dialog to close
WebDriverWait(driver, 20).until(EC.invisibility_of_element_located((By.XPATH, "//input[@type='file']")))
if __name__ == "__main__":
image_files_directory = r"E:\Desktop\social\Output_folder\folder 20"
# Prompt to focus on Firefox
print("Please focus on Firefox. The script will start in 5 seconds...")
time.sleep(5)
# Set up the Firefox driver
driver = webdriver.Firefox()
try:
# Open the website in a new tab
driver.get("UPLOAD_URL.com")
# Wait for the website to load and the target element to be clickable
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "(//div[@class='ms-Stack css-147']//div)[1]")))
# Click on the target element
target_element = driver.find_element(By.XPATH, "(//div[@class='ms-Stack css-147']//div)[1]")
target_element.click()
# Get a list of all PNG image files in the directory
image_files = [f for f in os.listdir(image_files_directory) if f.lower().endswith('.png')]
# Select image files for each tab
for image_file in image_files:
select_image_for_tab(driver, os.path.join(image_files_directory, image_file))
time.sleep(0.5) # Adjust this delay if needed
# Open a new tab and load the website in it
driver.execute_script("window.open('about:blank');")
driver.switch_to.window(driver.window_handles[-1])
driver.get("UPLOAD_URL.com")
# Wait for the website to load in the new tab and the target element to be clickable
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "(//div[@class='ms-Stack css-147']//div)[1]")))
time.sleep(4) # Additional wait time if needed
print("File selection completed for all tabs.")
except Exception as e:
print("An error occurred:", e)
finally:
# The script will not close the browser or any tabs after completion
pass
But every time I have to divide my files into groups of 20.
Firefox is not processing more than 20 tabs. How can I bypass this limit in remoted Firefox?
Note: Please don't ask about secret URL or UPLOAD_URL.com
答案1
得分: 1
我必须使用opts.set_preference("dom.popup_maximum", 50)
来将dom.popup_maximum
首选项设置为50。然后,在初始化webdriver.Firefox()
时,我必须使用opts
,如下所示:driver = webdriver.Firefox(options=opts)
。这样可以让Firefox同时处理多达50个弹出窗口(标签),这样您的脚本就可以处理更多数量的图像,而不会达到最大标签限制。
英文:
No need answer, I found solution.
I must set the dom.popup_maximum
preference to 50 using opts.set_preference("dom.popup_maximum", 50)
. Then I must use the opts
when initializing the webdriver.Firefox()
using driver = webdriver.Firefox(options=opts)
.
This allows Firefox to handle up to 50 popup windows (tabs) at a time, which should allow you to process a larger number of images in your script without hitting the maximum tab limit.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论