1

I'm working on a program that will help me select the 2nd option in the dropdown menu on https://www.pcfinancial.ca/ located in the upper-right corner of the window. Here's my code so far:

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import ui

driver = webdriver.Chrome() 

driver.get('https://www.pcfinancial.ca/');
driver.find_element_by_xpath('//*[@id="lnkSignInOp"]').click() #click on dropdown menu - working
driver.find_element_by_xpath('//*[@id="PCM"]/a').click() #select "pc mastercard" - not working

What's weird is I can access the dropdown menu using the first driver.find_elements_by_xpath(...) line but I get the following error when selecting the 2nd option.

Traceback (most recent call last):
  File "C:\Users\Imad\Documents\Programming\Python\test.py", line 10, in <module>
    driver.find_element_by_xpath('//*[@id="PCM"]/a').click() #open up dropdown menu works
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 501, in _execute
    return self._parent.execute(command, params)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 308, in execute
    self.error_handler.check_response(response)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
  (Session info: chrome=63.0.3239.84)
  (Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 10.0.16299 x86_64)

Can anyone help me understand what's going on and how to fix this? Thanks in advance!

2 Answers 2

2

This is the classic case of a timing issue. After you open the dropdown, wait for the menu link to be clickable with WebDriverWait and element_to_be_clickable expected condition:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)

driver.get('https://www.pcfinancial.ca/')
driver.find_element_by_xpath('//*[@id="lnkSignInOp"]').click()


wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#PCM a"))).click()
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot! I had no idea a wait time is involved after the webpage has actually loaded. As for WebDriverWait, do you know what unit the 10 is in? Just asking out of curiosity so I know what's "too long" next time
@ImadKalboneh no problem, glad it worked for you. 10 is in "seconds", thanks.
0

As per the WebSite the WebElement by the linkText PC MasterCard is within an <a> tag, so we will construct a unique xpath as follows :

from selenium import webdriver

driver = webdriver.Chrome() 
driver.get('https://www.pcfinancial.ca/');
driver.find_element_by_xpath('//*[@id="lnkSignInOp"]').click() #click on dropdown menu - working
driver.find_element_by_xpath("//a[@href='#PCM' and @rel='/re_register/index.html']").click() #click on "PC MasterCard" - now working
print("Clicked on PC MasterCard")

1 Comment

Thanks for the response, but that actually is one of the things I tried. Sadly, I got the same error. I got the answer from the previous person though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.