신종원
신종원

Reputation: 195

How can I print out list of drop down menu using Selenium Python?

I am trying to print out all the item in the first drop-down menu in this page (http://www.bobaedream.co.kr/cyber/CyberCar.php?gubun=I) with Selenium Python and its PhantomJS function. But it still returns No Attribute error message.

Please help.

My code snippet is like below:

#!/usr/bin/env python
#-*- coding: utf-8 -*-

import re

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import StaleElementReferenceException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select

from bs4 import BeautifulSoup
from time import sleep

link = 'http://www.bobaedream.co.kr/cyber/CyberCar.php?gubun=I'
driver = webdriver.PhantomJS()
driver.set_window_size(1920, 1080)
driver.get(link)
sleep(.75)

s = BeautifulSoup(driver.page_source, "html.parser", from_encoding='utf-8')

# open maker layer
next_elem = driver.find_element_by_xpath('//a[@title="제조사 선택"]')
next_elem.click()

# print maker
next_elem = driver.find_elements_by_tag_name('li')
print ("clicked maker", next_elem.text)
sleep(.75)

Upvotes: 0

Views: 1070

Answers (1)

alecxe
alecxe

Reputation: 473803

The next_elem contains a list of elements (because you've used find_elements_by_tag_name() method - note the "s") which does not have a .text attribute.

You can quickly fix it by using find_element_by_tag_name() to match a single element:

next_elem = driver.find_element_by_tag_name('li')

But, I think, you probably meant to get the list of makers which can be accessed via:

makers = driver.find_elements_by_css_selector("#layer_maker ul.list li a")
for maker in makers:
    print(maker.text)

Upvotes: 1

Related Questions