Home > Enterprise >  selenium click link inside ul list
selenium click link inside ul list

Time:05-16

I want to click a link(contain text="statement / invoice") that inside list that from the iframe, the output is no , any help please, thank you.

python.py

from selenium import webdriver
path = "C:\\Users\\user\\Downloads\\Chromedriver.exe"
driver = webdriver.Chrome(path)
driver.maximize_window()
driver.get("https://s.hongleongconnect.my/rib/app/fo/login?icp=hlb-en-all-header-txt-connectweb")


try:
    driver.switch_to.frame('idMainFrame')
    search = driver.find_element_by_xpath("//ul[@id='mega-menu]/li[2]").click()
    print('clicked')
except:
    print('no')

html

<iframe id="idMainFrame">......
<div class='contentwrapper'>
   <div class='container'>
      <div class='sixteen columns'>
         <ul id='mega-menu' class='nomach full-width'>
            <li class='ismach'>...</li>
            <li class='nomach dc-mega-li'>...</li>
            <li class='nomach dc-mega-li'>
               <a href='#' class='statementnomach dc-mega'>
                 "STATEMENT /"
                 <br>
                 "INVOICE"
               </a>
            </li>
         </ul>
      </div>
   </div>
</div>
</iframe>

CodePudding user response:

First of all you should run it without the try block and see what error is thrown. To target that specific link you could do:

search = driver.find_element(By.PARTIAL_LINK_TEXT, 'STATEMENT /').click()

or use By.LINK_TEXT and specify the exact value.

CodePudding user response:

The url is dynamic. So you can use selenium with bs4

from bs4 import BeautifulSoup
import requests

html_text = requests.get('http://www.edwaittimes.ca/WaitTimes.aspx')
#print(html_text)
soup = BeautifulSoup(html_text.content, 'lxml')
hospital_table = soup.select('div.CellfcW2 > p > a')
hospital_table = soup.find_all('div',class_="Row")
for hospital in hospital_table:
    if hospital.a is not None:
        print(hospital.a.text)

Output:

Mount Saint Joseph Hospital
UBC Hospital (UBCH)
City Centre Urgent & Primary Care Centre
Vancouver General Hospital
St. Paul's Hospital
REACH Urgent and Primary Care Centre
Northeast Urgent and Primary Care Centre
Southeast Urgent and Primary Care Centre
BC Children's Hospital
Richmond Hospital
Richmond Urgent and Primary Care Centre
Squamish General Hospital
Whistler Health Care Centre
Lions Gate Hospital
Sechelt Hospital
Pemberton Health Centre
North Van Urgent & Primary Care Centre
  • Related