на сайте https://isapps.acxiom.com/optout/optout.aspx#section8 Я хочу получить доступ к полю «Кто отказывается?». Использование логики c сообщения Python Selenium webdriver: элемент не взаимодействует: Элемент в настоящее время не виден и не может быть изменен Я попробовал следующий код Версия 1:
ele2 = driver.find_element_by_xpath("//select[@id='Identity']/option[@value='Myself']")
driver.execute_script("arguments[0].click()",ele2)
Версия 2:
driver.find_element_by_xpath ("// select [@ id = 'Identity'] / option [@ value = 'Myself']"). Click ()
Ошибка Я получаю это:
Traceback (most recent call last):
File "website-functions/acxiom.py", line 51, in <module>
acxiom_DD_formfill(title, firstname, middlename, lastname, suffix, email)
File "website-functions/acxiom.py", line 30, in acxiom_DD_formfill
driver.find_element_by_xpath("//select[@id='Identity']/option[@value='Myself']").click()
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 978, in find_element
'value': value})['value']
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.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//select[@id='Identity']/option[@value='Myself']"}
(Session info: headless chrome=80.0.3987.87)
Это не имеет смысла для меня, поскольку идентификатор действительно «Идентичность» (проверьте по https://isapps.acxiom.com/optout/optout.aspx#section8).
Вот полный код, который я использовал:
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.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")
#Select identity: Following option values: "Myself", "Legal guardian", "Deceased person"
#ele2 = driver.find_element_by_xpath("//select[@id='Identity']/option[@value='Myself']")
#driver.execute_script("arguments[0].click()",ele2)
"""Version 2"""
#driver.find_element_by_xpath("//select[@id='Identity']/option[@value='Myself']").click()
dropdown_optoutchoice=driver.find_element_by_id("'Identity'").location_once_scrolled_into_view
dropdown_optoutchoice.select_by_value('Myself')
# 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)
Спасибо за вашу помощь!