Reputation: 35
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!
Upvotes: 1
Views: 346
Reputation: 193058
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")
Upvotes: 0
Reputation: 473753
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()
Upvotes: 2