Python Selenium webdriver:元素不可交互:元素当前不可见,可能无法操作

时间:2020-03-24 16:41:52

标签: python selenium selenium-webdriver webdriver

我编写了以下代码来与Acxiom网站进行交互:

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)
    river = 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
    driver.get("https://isapps.acxiom.com/optout/optout.aspx#section8")
    #Select opt out segment: Following option values: "Mail", "Telemarketing", "Email"
    dropdown_optoutchoice= Select(driver.find_element_by_id("OptOutChoices2"))
    dropdown_optoutchoice.select_by_value('Mail')
    print("dropdown selected")
    # KEEP THIS DISABLED BC IT ACTUALLY SUBMITS 
    # driver.find_element_by_id("SubmitButton2").send_keys(Keys.ENTER) # submit button
    print("executed")
    time.sleep(4)
    driver.quit()
    return None

我正在尝试在以下网站https://isapps.acxiom.com/optout/optout.aspx#section8上选择“多项选择”选项“邮件”:

<select size="4" name="OptOutChoices2" multiple="multiple" id="OptOutChoices2" class="optOutChoices" tabindex="-1" data-ssid="ss-78884" style="display: none;">
        <option value="Mail">Mailing Addresses</option>
        <option value="Telemarketing">Phone Numbers</option>
        <option value="Email">Email Addresses</option>

    </select>

但是,这会产生以下错误:

opening data delete form
Traceback (most recent call last):
  File "website-functions/acxiom.py", line 38, in <module>
    acxiom_DD_formfill(title, firstname, middlename, lastname, suffix, email)
  File "website-functions/acxiom.py", line 24, in acxiom_DD_formfill
    dropdown_optoutchoice.select_by_value('Mail')
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/support/select.py", line 82, in select_by_value
    self._setSelected(opt)
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/support/select.py", line 212, in _setSelected
    option.click()
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  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.ElementNotInteractableException: Message: element not interactable: Element is not currently visible and may not be manipulated
  (Session info: headless chrome=80.0.3987.87)

谢谢您的帮助!

-------编辑1 ------- 运行driver.find_element_by_xpath("//select[@id='OptOutChoices2']/option[@value='Mail']").click()

时出现完全相同的错误

代替

  dropdown_optoutchoice= Select(driver.find_element_by_id("OptOutChoices2"))
  dropdown_optoutchoice.select_by_value('Mail')

0 个答案:

没有答案