无法在使用无头Chrome Driver的Selenium中创建帐户。

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

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.

Successful message after running pytest

答案1

得分: 1

不需要翻译的代码部分:

Instead of:

chrome_options.add_argument("--headless")

try:

chrome_options.add_argument("--headless=new")

注意:

要使用本机无头模式,即**--headless=new,官方现在称之为新的无头模式**,需要满足以下先决条件:

通过以下方式提供支持:

参考资料:

您可以在以下几个相关详细讨论中找到更多信息:

英文:

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:

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

发表评论

匿名网友

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

确定