为什么Firefox的Selenium WebDriver不能处理超过20个标签页?

huangapple go评论114阅读模式
英文:

Why is Firefox selenium webdriver not processing more then 20 tabs?

问题

我正在使用以下脚本将我的PNG图像上传到一个秘密网站:

  1. from selenium import webdriver
  2. from selenium.webdriver.common.by import By
  3. from selenium.webdriver.support.ui import WebDriverWait
  4. from selenium.webdriver.support import expected_conditions as EC
  5. import time
  6. import os
  7. def select_image_for_tab(driver, image_file):
  8. # 查找并点击“浏览文件”按钮
  9. browse_button = driver.find_element(By.XPATH, "//button[text()='Browse for a file']")
  10. browse_button.click()
  11. # 等待文件输入框可见并可交互
  12. time.sleep(0.5)
  13. # 使用Selenium处理文件上传对话框
  14. file_input = driver.find_element(By.XPATH, "//input[@type='file']")
  15. file_input.send_keys(image_file)
  16. # 等待文件上传完成并关闭文件选择对话框
  17. WebDriverWait(driver, 20).until(EC.invisibility_of_element_located((By.XPATH, "//input[@type='file']")))
  18. if __name__ == "__main__":
  19. image_files_directory = r"E:\Desktop\social\Output_folder\folder 20"
  20. # 提示关注Firefox
  21. print("请关注Firefox。脚本将在5秒后开始...")
  22. time.sleep(5)
  23. # 设置Firefox驱动
  24. driver = webdriver.Firefox()
  25. try:
  26. # 在新标签页中打开网站
  27. driver.get("UPLOAD_URL.com")
  28. # 等待网站加载和目标元素可点击
  29. WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "(//div[@class='ms-Stack css-147']//div)[1]")))
  30. # 点击目标元素
  31. target_element = driver.find_element(By.XPATH, "(//div[@class='ms-Stack css-147']//div)[1]")
  32. target_element.click()
  33. # 获取目录中所有PNG图像文件的列表
  34. image_files = [f for f in os.listdir(image_files_directory) if f.lower().endswith('.png')]
  35. # 为每个标签页选择图像文件
  36. for image_file in image_files:
  37. select_image_for_tab(driver, os.path.join(image_files_directory, image_file))
  38. time.sleep(0.5) # 如果需要,调整此延迟
  39. # 打开新标签页并加载网站
  40. driver.execute_script("window.open('about:blank');")
  41. driver.switch_to.window(driver.window_handles[-1])
  42. driver.get("UPLOAD_URL.com")
  43. # 等待新标签页中的网站加载和目标元素可点击
  44. WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "(//div[@class='ms-Stack css-147']//div)[1]")))
  45. time.sleep(4) # 如果需要,添加额外等待时间
  46. print("已为所有标签页完成文件选择。")
  47. except Exception as e:
  48. print("发生错误:", e)
  49. finally:
  50. # 脚本完成后不会关闭浏览器或任何标签页
  51. pass

但是,每次我都需要将文件分成20个一组。Firefox不能处理超过20个标签页。如何在远程Firefox中绕过这个限制?

注意:请不要询问秘密URL或UPLOAD_URL.com

英文:

I am using the following script to upload my png images to a secret website:

  1. from selenium import webdriver
  2. from selenium.webdriver.common.by import By
  3. from selenium.webdriver.support.ui import WebDriverWait
  4. from selenium.webdriver.support import expected_conditions as EC
  5. import time
  6. import os
  7. def select_image_for_tab(driver, image_file):
  8. # Find and click on "Browse for a file" button
  9. browse_button = driver.find_element(By.XPATH, "//button[text()='Browse for a file']")
  10. browse_button.click()
  11. # Wait for the file input to be visible and interactable
  12. time.sleep(0.5)
  13. # Handle the file upload dialog using Selenium
  14. file_input = driver.find_element(By.XPATH, "//input[@type='file']")
  15. file_input.send_keys(image_file)
  16. # Wait for the file to be uploaded and the file selection dialog to close
  17. WebDriverWait(driver, 20).until(EC.invisibility_of_element_located((By.XPATH, "//input[@type='file']")))
  18. if __name__ == "__main__":
  19. image_files_directory = r"E:\Desktop\social\Output_folder\folder 20"
  20. # Prompt to focus on Firefox
  21. print("Please focus on Firefox. The script will start in 5 seconds...")
  22. time.sleep(5)
  23. # Set up the Firefox driver
  24. driver = webdriver.Firefox()
  25. try:
  26. # Open the website in a new tab
  27. driver.get("UPLOAD_URL.com")
  28. # Wait for the website to load and the target element to be clickable
  29. WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "(//div[@class='ms-Stack css-147']//div)[1]")))
  30. # Click on the target element
  31. target_element = driver.find_element(By.XPATH, "(//div[@class='ms-Stack css-147']//div)[1]")
  32. target_element.click()
  33. # Get a list of all PNG image files in the directory
  34. image_files = [f for f in os.listdir(image_files_directory) if f.lower().endswith('.png')]
  35. # Select image files for each tab
  36. for image_file in image_files:
  37. select_image_for_tab(driver, os.path.join(image_files_directory, image_file))
  38. time.sleep(0.5) # Adjust this delay if needed
  39. # Open a new tab and load the website in it
  40. driver.execute_script("window.open('about:blank');")
  41. driver.switch_to.window(driver.window_handles[-1])
  42. driver.get("UPLOAD_URL.com")
  43. # Wait for the website to load in the new tab and the target element to be clickable
  44. WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "(//div[@class='ms-Stack css-147']//div)[1]")))
  45. time.sleep(4) # Additional wait time if needed
  46. print("File selection completed for all tabs.")
  47. except Exception as e:
  48. print("An error occurred:", e)
  49. finally:
  50. # The script will not close the browser or any tabs after completion
  51. 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.

huangapple
  • 本文由 发表于 2023年7月28日 03:06:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76782739.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定