英文:
Cannot create account in Selenium with Headless Chrome Driver
问题
我正在尝试使用Selenium创建用户,因为我无法从服务器访问数据库。我收到"PASS"消息,但用户从未创建,所以我认为可能与无头浏览器有关。
是否有办法在无头模式下使用点击和填写表单?
设置
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-gpu')
chrome_service = ChromeService('/home/bitnami/chromedriver')
driver = webdriver.Chrome(service=self.chrome_service, options=self.chrome_options)
创建用户
driver.get(URL)
driver.set_window_size(1657, 1006)
driver.find_element(By.ID, "username").send_keys(USER)
driver.find_element(By.ID, "password").send_keys(PASS)
driver.find_element(By.ID, "_submit").click()
driver.find_element(By.LINK_TEXT, "Ajustes").click()
driver.find_element(By.CSS_SELECTOR, "#administrator-salespersons span").click()
driver.find_element(By.CSS_SELECTOR, ".fa-plus").click()
driver.find_element(By.CSS_SELECTOR, ".btn-sm").click()
driver.find_element(By.ID, "sal_username").send_keys(USER2)
driver.find_element(By.ID, "sal_password").send_keys(PASS2)
driver.find_element(By.ID, "sal_email").send_keys(EMAIL)
driver.find_element(By.ID, "sal_Guardar").click()
driver.quit()
注意: 我已在我的计算机上测试了没有无头浏览器的情况,它成功创建了用户。在服务器上我没有收到任何错误。我还在互联网上搜索过,但找到的示例都没有包含点击操作。
英文:
I am trying to create a user with Selenium because I don't have access to the database from my server. I get the message "PASS" but the user is never created so I thing it might be something related to headless.
Is there any way to use clicks and filling forms with headless?
Setup
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-gpu')
chrome_service = ChromeService('/home/bitnami/chromedriver')
driver = webdriver.Chrome(service=self.chrome_service,options=self.chrome_options)
Create user
driver.get(URL)
driver.set_window_size(1657, 1006)
driver.find_element(By.ID, "username").send_keys(USER)
driver.find_element(By.ID, "password").send_keys(PASS)
driver.find_element(By.ID, "_submit").click()
driver.find_element(By.LINK_TEXT, "Ajustes").click()
driver.find_element(By.CSS_SELECTOR, "#administrator-salespersons span").click()
driver.find_element(By.CSS_SELECTOR, ".fa-plus").click()
driver.find_element(By.CSS_SELECTOR, ".btn-sm").click()
driver.find_element(By.ID, "sal_username").send_keys(USER2)
driver.find_element(By.ID, "sal_password").send_keys(PASS2)
driver.find_element(By.ID, "sal_email").send_keys(EMAIL)
driver.find_element(By.ID, "sal_Guardar").click()
driver.quit()
Note: I've tested in my computer without headless and it creates the user successfully. I don't get any error in the server. I've also searched all over the internet but all I've found are examples without clicks.
答案1
得分: 1
不需要翻译的代码部分:
Instead of:
chrome_options.add_argument("--headless")
try:
chrome_options.add_argument("--headless=new")
注意:
要使用本机无头模式,即**--headless=new
,官方现在称之为新的无头模式**,需要满足以下先决条件:
通过以下方式提供支持:
参考资料:
您可以在以下几个相关详细讨论中找到更多信息:
- DeprecationWarning: headless property is deprecated, instead use add_argument(''--headless'') or add_argument(''--headless=new'') on Selenium 4.8.0 Python
- Updating Selenium Python to v4.8.0 "options.headless = True" shows DeprecationWarning
英文:
Instead of:
chrome_options.add_argument("--headless")
try:
chrome_options.add_argument("--headless=new")
Note
The prerequisite to use Native Headless mode i.e. --headless=new
which is now officially called the new Headless mode are as follows:
aptly supported through:
References
You can find a couple of relevant detailed discussions in:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论