Platalea Minor
Platalea Minor

Reputation: 877

ElementNotVisibleException Error with xpath selenium python

Hi I am scraping a website and I am using selenium python. The code is as below

url ='https://www.hkexnews.hk/'

options = webdriver.ChromeOptions()
browser = webdriver.Chrome(chrome_options=options, executable_path=r'chromedriver.exe')
browser.get(url)

tier1 = browser.find_element_by_id('tier1-select')
tier1.click()

tier12 = browser.find_element_by_xpath('//*[@data-value="rbAfter2006"]')
tier12.click()
    
time.sleep(1)
    
tier2 = browser.find_element_by_id('rbAfter2006')
tier2.click()
    
tier22 = browser.find_element_by_xpath("//*[@id='rbAfter2006']//*[@class='droplist-item droplist-item-level-1']//*[text()='Circulars']")
tier22.click()

tier23 = browser.find_element_by_xpath("//*[@id='rbAfter2006']//*[@class='droplist-item droplist-item-level-2']//*[text()='Securities/Share Capital']")
tier23.click()

tier24 = browser.find_element_by_xpath("//*[@id='rbAfter2006']//*[@class='droplist-group droplist-submenu level3']//*[text()='Issue of Shares']")
tier24.click()

It works stops at tier23 by showing ElementNoVisibleException. I have tried with different class, yet it seems like not working. Thank you for you help

Upvotes: 0

Views: 42

Answers (1)

JaSON
JaSON

Reputation: 4869

There are two elements that can be selected by your XPath. The first one is hidden. Try below to select required element:

tier23 = browser.find_element_by_xpath("//*[@id='rbAfter2006']//li[@aria-expanded='true']//*[@class='droplist-item droplist-item-level-2']//*[text()='Securities/Share Capital']")

or shorter

tier23 = browser.find_element_by_xpath("//li[@aria-expanded='true']//a[.='Securities/Share Capital']")
tier23.location_once_scrolled_into_view
tier23.click()

P.S. Note that that option will still be not visible because you need to scroll list down first. I used tier23.location_once_scrolled_into_view for this purpose

Also it's better to use Selenium built-in Waits instead of time.sleep

Upvotes: 2

Related Questions