Using Selenium to select a button to open a dropdown window

54 Views Asked by At

I’m trying to a script to pull reports from a client’s site here: https://ww360.azurewebsites.net/account/login, and I’m trying to get the script to select “change” to make the pop up window come up and select the particular location to pull reports for, and I haven’t been able to get it to work. Tried selecting by xpath, css, text, and link, won’t go through.

#Import packages
from selenium import webdriver
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from datetime import datetime
from selenium.webdriver.common.keys import Keys
import win32com.client
import glob
import os
from selenium.webdriver.support.ui import Select


# set directory and import drivers
chrome_options = webdriver.ChromeOptions()
prefs = {'download.default_directory' : r'C:\Users\Austin Vanderlyn\Downloads',  "download.prompt_for_download": False}
chrome_options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(service=Service(executable_path=r"G:\Analytics\_chromedriver\chromedriver.exe"), options=chrome_options)


# opens a chrome window to go to the window world portal
time.sleep(5)
driver.get("https://ww360.azurewebsites.net/account/login")
time.sleep(5)


# select the change button
element=WebDriverWait(driver,30).until(
EC.element_to_be_clickable((By.XPATH, "/html/body/app-root/ng-component/div/div/div[1]/div/div/div[1]/div[2]/tenant-change/span/a")))
element.click()

Does anyone know why this is not working?

2

There are 2 best solutions below

0
Shawn On

You are very close. You are getting below exception:

selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <a href="javascript:;">...</a> is not clickable at point (278, 207). Other element would receive the click: <div class="m-login__logo">...</div>

Problem: Another element is obstructing your click() action.

Solution1: Just maximise the window before performing the click. Use maximize_window() code after launching the URL, see below:

driver.get("https://ww360.azurewebsites.net/account/login")
driver.maximize_window()

Solution2: If despite maximising the window issue persist, then try to perform click() using JavaScript as below:

element=WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH, "//a[text()='Change']")))
driver.execute_script("arguments[0].click();", element)
# element.click()

On another note, SUGGESTION: Try to avoid using absolute XPath expressions and use relative XPaths, as the latter are more reliable.

Change the below:

/html/body/app-root/ng-component/div/div/div[1]/div/div/div[1]/div[2]/tenant-change/span/a

TO:

//a[text()='Change']
0
BernardV On

You can try improving your Xpath first. Try and never use auto generated Xpath's as they tend to be flaky and harder to maintain overall.

xpath_str = \
    '//div[@class="content tenant-change-box ng-star-inserted"]//a'

element = WebDriverWait(driver,
                        20).until(EC.element_to_be_clickable((By.XPATH,
                                  xpath_str)))

element. Click()