0

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

2
  • Which is tier23 exactly? Commented Nov 7, 2019 at 6:31
  • tier23 is the the submenu under Circulars The button flow is Circulars -> Securities/Share Capital -> Issue of Shares Commented Nov 7, 2019 at 6:36

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

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.