Home > Net >  How do i handle this dropdown using selenium with no select class?
How do i handle this dropdown using selenium with no select class?

Time:07-16

I am trying to access this website and after that, I want to click on the Status dropdown and select Active from the dropdown. I think there are no select tags to be used hence the traditional select method is not working. Would be really helpful if anyone can have a look at it!

"""

PATH = "/usr/local/bin/chromedriver"
driver = webdriver.Chrome(PATH)
driver.get("https://www.silveroaksp.com/portfolio")
driver.find_element_by_xpath("//div[@class='btn-group show']").click()
time.sleep(2)
driver.close()

"""

CodePudding user response:

*To capture all the Webelements in dropdown we use driver.findElements for multiple elements(this return element in the list). *You can use to method for this. click on Feild and then click on active Web element, Second is you put it in a for each loop and in IFELSE condition put click

1.

 PATH = "/usr/local/bin/chromedriver"
driver = webdriver.Chrome(PATH)
driver.get("https://www.silveroaksp.com/portfolio")
driver.find_element_by_xpath("//select[@id='StatusMultiselect']/following- 
sibling::div").click()
time.sleep(2)
driver.find_element_by_xpath("//select[@id='StatusMultiselect']/following- 
sibling::div/ul/li[2]/a/label/input").click()
time.sleep(2)
driver.close()

*This'll work I guess. IF doesn't let me know I'll provide a second option

CodePudding user response:

The problem can be solved via Requests:

import requests
from bs4 import BeautifulSoup

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:23.0) Gecko/20131011 Firefox/23.0', 
           'Host': 'www.silveroaksp.com', 
           'Accept': 'text/html, */*; q=0.01', 
           'Accept-Encoding': 'gzip, deflate, br', 
           'Referer': 'https://www.silveroaksp.com/portfolio', 
           'Content-Type': 'application/json; charset=utf-8', 
           'X-Requested-With': 'XMLHttpRequest', 
           'Content-Length': '144', 
           'Origin': 'https://www.silveroaksp.com', 
           'Connection': 'keep-alive', 
           'Sec-Fetch-Dest': 'empty', 
           'Sec-Fetch-Mode': 'cors', 
           'Sec-Fetch-Site': 'same-origin', 
           'TE': 'trailers'}

data = '{"prmStatusIds":"PortStatus1","prmIndustryIds":"","prmFundIds":"","AllStatusClickChecker":"0","AllIndustryClickChecker":"1","AllFundClickChecker":"1"}'
with requests.Session() as s:
    s.get('https://www.silveroaksp.com/portfolio')

    
    r = s.post('https://www.silveroaksp.com/CommonItem/FilterPort', data=data, headers=headers)
soup = BeautifulSoup(r.text, 'html.parser')
actives = soup.select('div.col-lg-4')
for active in actives:
    business = active.select_one('img')['alt']
    print(active.text.strip(), business)

This returns:

Active  | Fund III   | Business Services Brilliant
Active  | Fund III   | Consumer Services Caring People
Active  | Fund IV   | Business Services CCS Facility Services
Active  | Fund IV   | Consumer Services Drive Automotive Services
[...]
  • Related