gpt4 book ai didi

Python Selenium 屏幕抓取

转载 作者:太空宇宙 更新时间:2023-11-03 18:21:11 24 4
gpt4 key购买 nike

我正在尝试屏幕抓取网站(下面的片段)

网站接受输入,导航到第二页并接受更多输入,最后显示一个表格。我在这一步失败了:

driver.find_element_by_xpath("//select[@id='agencies']/option[@value='13156']").click()

我得到的错误是:

selenium.common.exceptions.NoSuchElementException: Message: 'Unable to locate element: 

这很奇怪,因为我确实看到了该元素(注释掉了显示 ID)。有什么帮助/指点吗?

(我尝试了 requests/RoboBrowser —— 似乎无法让帖子正常工作,但也失败了)

from selenium import webdriver
from selenium import selenium
from bs4 import BeautifulSoup

driver = webdriver.Firefox()
url = 'http://www.ucrdatatool.gov/Search/Crime/Local/OneYearofData.cfm'
driver.get(url)

driver.find_element_by_xpath("//select[@id='state']/option[@value='1']").click()
#driver.find_element_by_xpath("//select[@id='groups']/option[@value='8']").click()

driver.find_element_by_xpath("//input[@type='submit' and @value='Next']").click()
driver.implicitly_wait(5) # seconds

# Display id tags
#elementsAll = driver.find_elements_by_xpath('//*[@id]')
#for elements in elementsAll:
# print("id: ", repr(elements))
# print("idName: ",elements.get_attribute("id"))
# driver.implicitly_wait(5) # seconds

driver.find_element_by_xpath("//select[@id='groups']/option[@value='2']").click()
driver.find_element_by_xpath("//select[@id='year']/option[@value=1986]").click()
driver.find_element_by_xpath("//select[@id='agencies']/option[@value='13156']").click()

更新——下面的适用于Selenium。我打算选择列表框中的所有选项并保存查询结果...感谢您的指点,Alecxe!

select = Select(driver.find_element_by_id('agencies'))
for options in select.options:
select.select_by_visible_text(options.text)

select = Select(driver.find_element_by_id('groups'))
for options in select.options:
select.select_by_visible_text(options.text)

driver.find_element_by_xpath("//select[@id='year']/option[@value=1985]").click()

driver.find_element_by_xpath("//input[@type='submit' and @value='Get Table']").click()

最佳答案

ID 为 agentsselect 中没有值为 13156option。有从102522的值,你可以通过打印看到它们:

[element.get_attribute('value') for element in driver.find_elements_by_xpath('//select[@id="agencies"]/option')] 

此外,不要通过查找选项,而是使用Select并通过文本获取选项:

from selenium.webdriver.support.ui import Select

select = Select(driver.find_element_by_id('agencies'))
print select.options
select.select_by_visible_text('Selma Police Dept')

关于Python Selenium 屏幕抓取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24111681/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com