在网站https://isapps.acxiom.com/optout/optout.aspx#section8中,我要访问“谁选择退出?”字段。使用帖子Python Selenium webdriver: element not interactable: Element is not currently visible and may not be manipulated的逻辑,我尝试了以下代码 版本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)
这对我来说没有意义,因为ID确实是“身份”(请检查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)
谢谢您的帮助!
答案 0 :(得分:0)
这是正确的xpath。
//select[@name='Identity']/option[@value = 'Submitter']
value
属性值是Submitter
而不是Myself
,它是选项节点的文本。这就是为什么您会收到错误消息。
ele = driver.find_element_by_xpath("//select[@name='Identity']/option[@value = 'Submitter']")
driver.execute_script("arguments[0].click()",ele) # using js click so that item will be selected though it's not visible.
答案 1 :(得分:0)
请参考以下解决方案以从下拉框中选择值。您可以传递选项值,然后使用下拉菜单选择它。
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Chrome("C:\New folder\chromedriver.exe")
driver.get("https://isapps.acxiom.com/optout/optout.aspx#section8")
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//select[@id='Identity']/option[contains(text(),'Who is opting out?')]"))).click();