datasciencelearner
datasciencelearner

Reputation: 185

Python selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element

within the website https://isapps.acxiom.com/optout/optout.aspx#section8 I want to access the field “Who is opting out?”. Using the logic of the post Python Selenium webdriver: element not interactable: Element is not currently visible and may not be manipulated I tried following code Version 1:

 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()

The error I get is:

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)

This does not make sense to me since the id is indeed “Identity” (check at https://isapps.acxiom.com/optout/optout.aspx#section8).

Here is the full code I used:

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 = "[email protected]"
acxiom_DD_formfill(title, firstname, middlename, lastname, suffix, email)

Thank you for your help!

Upvotes: 0

Views: 5899

Answers (2)

SeleniumUser
SeleniumUser

Reputation: 4177

Please refer below solution to select value from dropdown box. You can pass your option value and select it using drop down.

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();

Upvotes: 2

supputuri
supputuri

Reputation: 14135

Here is the correct xpath.

//select[@name='Identity']/option[@value = 'Submitter']

Screenshot: enter image description here

value attribute value is Submitter not Myself, which is text of the option node. That's why you are getting the error.

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.

Upvotes: 0

Related Questions