Python 用 selenium 提取浏览器下拉列表
Python extract a browser dropdown list with selenium
亲爱的,
我是 python 中使用 selenium 进行网页抓取的新手。
现在我有一个简单的例子(附图片)我想从下拉列表“Select国家”
中提取所有国家
我做了以下代码
driver = webdriver.Chrome(path)
driver.get(website)
wait = 20
countriesdropdown = driver.find_element_by_xpath('//*[@id="dropdown"]/ul/li/a')
print(countriesdropdown)
但我在发件箱中收到一些不理解的内容。
<selenium.webdriver.remote.webelement.WebElement (session="379a6b651a4829939ee2907a649d7655", element="3942d4ab-bb74-407a-a673-886d11fe49e9")>
能否请您帮助我最好的方法,并在 python 中了解有关使用 selenium 进行网页抓取的更多信息?
谢谢,
梅尔狗
这里有几个问题。
driver.find_element_by_xpath('//*[@id="dropdown"]/ul/li/a')
returns 单个元素,不是你想要的列表。
要获取网络元素列表,您应该使用 driver.find_elements_by_xpath('//*[@id="dropdown"]/ul/li/a')
- 当你有一个列表时,你将不得不遍历列表元素并获取它们的文本。
像这样:
countries = driver.find_elements_by_xpath('//*[@id="dropdown"]/ul/li/a')
for country in countries:
print(country.text)
亲爱的,
我是 python 中使用 selenium 进行网页抓取的新手。
现在我有一个简单的例子(附图片)我想从下拉列表“Select国家”
中提取所有国家我做了以下代码
driver = webdriver.Chrome(path)
driver.get(website)
wait = 20
countriesdropdown = driver.find_element_by_xpath('//*[@id="dropdown"]/ul/li/a')
print(countriesdropdown)
但我在发件箱中收到一些不理解的内容。
<selenium.webdriver.remote.webelement.WebElement (session="379a6b651a4829939ee2907a649d7655", element="3942d4ab-bb74-407a-a673-886d11fe49e9")>
能否请您帮助我最好的方法,并在 python 中了解有关使用 selenium 进行网页抓取的更多信息?
谢谢, 梅尔狗
这里有几个问题。
driver.find_element_by_xpath('//*[@id="dropdown"]/ul/li/a')
returns 单个元素,不是你想要的列表。
要获取网络元素列表,您应该使用driver.find_elements_by_xpath('//*[@id="dropdown"]/ul/li/a')
- 当你有一个列表时,你将不得不遍历列表元素并获取它们的文本。
像这样:
countries = driver.find_elements_by_xpath('//*[@id="dropdown"]/ul/li/a')
for country in countries:
print(country.text)