Я бы хотел посмотреть, что делает моя программа Selenium Webdriver. Тем не менее, в настоящее время я использую безголовый Chrome, и он не работает в своем собственном окне. Как мне запустить программу в окне, которое я вижу?
Это код, который я использую:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.chrome.options import Options
import os
import time
def acxiom_DD_formfill(title, firstname, middlename, lastname, suffix, email):
chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=chrome_options)
#driver = webdriver.Chrome('/usr/lib/chromium-browser/chromedriver', options=chrome_options)
driver.set_page_load_timeout(10)
driver.set_window_size(1124, 850) # set browser size.
# link to data delete form
print("opening data delete form")
driver.get("https://isapps.acxiom.com/optout/optout.aspx#section8")
#Select opt out segment: Following option values: "Mail", "Telemarketing", "Email"
ele = driver.find_element_by_xpath("//select[@id='OptOutChoices2']/option[@value='Mail']")
driver.execute_script("arguments[0].click()",ele)
print("dropdown selected")
time.sleep(4)
driver.quit()
return None
Если не включать следующую строку: #chrome_options.add_argument('--headless')
Я получаю эту ошибку:
Traceback (most recent call last):
File "website-functions/acxiom.py", line 46, in <module>
acxiom_DD_formfill(title, firstname, middlename, lastname, suffix, email)
File "website-functions/acxiom.py", line 29, in acxiom_DD_formfill
dropdown_optoutchoice.select_by_value('Myself')
AttributeError: 'dict' object has no attribute 'select_by_value'
alexanderfarr@DESKTOP-4TE1ODA:/mnt/c/Users/Alexa/OneDrive/Backend-web-architecture/github-repos/privacy-api-backend$ python3 website-functions/acxiom.py
Traceback (most recent call last):
File "website-functions/acxiom.py", line 46, in <module>
acxiom_DD_formfill(title, firstname, middlename, lastname, suffix, email)
File "website-functions/acxiom.py", line 15, in acxiom_DD_formfill
driver = webdriver.Chrome(options=chrome_options)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
desired_capabilities=desired_capabilities)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally.
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location /usr/bin/chromium-browser is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
Спасибо за вашу помощь!
----------------------- EDIT --------------------------------------------
Когда вместо этого используется следующий код:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
import os
import time
def acxiom_DD_formfill(title, firstname, middlename, lastname, suffix, email):
chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
#chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=chrome_options)
driver.set_page_load_timeout(10)
driver.set_window_size(1124, 850) # set browser size
# link to data delete form
print("opening data delete form")
driver.get("https://isapps.acxiom.com/optout/optout.aspx#section8")
#Select opt out segment: Following option values: "Mail", "Telemarketing", "Email"
dropdown=WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CLASS_NAME, "ss-multi-selected")))
dropdown.click()
WebDriverWait(driver,10).until(EC.visibility_of_all_elements_located((By.CLASS_NAME,'ss-option')))
list_options = driver.find_elements_by_class_name('ss-option')
list_options[2].click()
print("dropdown selected")
#Select identity: Following option values: "Myself", "Legal guardian", "Deceased person"
identity_dropdown = driver.find_element_by_id('Identity')
select = Select(identity_dropdown)
select.select_by_value('Submitter')
#Select title: Following option values: "Mr.", "Mrs.", "Ms.", "Dr.", "Honorable", "Reverend", "Other"
title_dropdown=driver.find_element_by_id('Title')
select = Select(title_dropdown)
select.select_by_value(title)
driver.find_element_by_id("FirstName").send_keys(firstname)
driver.find_element_by_id("MiddleName").send_keys(middlename)
driver.find_element_by_id("LastName").send_keys(lastname)
driver.find_element_by_id("DDSuffix").send_keys(suffix)
driver.find_element_by_id("Email").send_keys(email)
# KEEP THIS DISABLED BC IT ACTUALLY SUBMITS
# driver.find_element_by_id("SubmitButton2").send_keys(Keys.ENTER)
print("executed")
time.sleep(4)
driver.quit()
return None
title = "Mr."
middlename = ""
firstname = "Joe"
lastname = "Musterman"
suffix = ""
email = "joe@musterman.com"
acxiom_DD_formfill(title, firstname, middlename, lastname, suffix, email)
Я получаю эту ошибку, которая, похоже, не связана с самим сценарием:
Traceback (most recent call last):
File "other/acxiom-test.py", line 64, in <module>
acxiom_DD_formfill(title, firstname, middlename, lastname, suffix, email)
File "other/acxiom-test.py", line 21, in acxiom_DD_formfill
driver = webdriver.Chrome(options=chrome_options)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
desired_capabilities=desired_capabilities)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally.
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location /usr/bin/chromium-browser is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
Я не уверен, как Я мог бы исправить эту ошибку.