我在选择下拉按钮,然后选择其他选项来更改网页时遇到一些问题。我在Python中使用Selenium来提取这些数据。 URL是 https://www.transfermarkt.com/premierleague/startseite/wettbewerb/GB1/plus/?saison_id=2019
迄今为止的代码:
driver = webdriver.Chrome('C:/Users/bzholle/chromedriver.exe')
driver.get('https://www.transfermarkt.com/premierleague/startseite/wettbewerb/GB1/plus/?saison_id=2019')
#click out of iframe pop-up window
driver.switch_to.frame(driver.find_element_by_css_selector('iframe[title="SP Consent Message"]'))
accept_button = driver.find_element_by_xpath("//button[@title='ACCEPT ALL']")
accept_button.click()
driver.find_element_by_id("choosen-country").click()
我不断得到: NoSuchElementException:消息:没有这样的元素:无法找到元素
在HTML代码中,只有单击下拉箭头,国家列表才会出现;然而,无论我如何无法点击按钮。有人有什么建议吗
你忘了提到你实际上想搜集什么信息,所以我建议的以下替代解决方案只能帮你这么多。如果您能详细说明,并让我知道您试图获取的信息,我可以定制我的解决方案
记录ones的网络流量(在浏览器中查看页面时)会发现向各种REST API端点发出了多个XHR(XmlHttpRequest)HTTP GET请求,其响应是JSON,并且包含您可能想要获取的所有信息
我的建议是简单地模拟对必要的RESTAPI端点的HTTP GET请求。无需硒:
输出:
编辑-不幸的是,您试图获取的表数据并非来自API。它直接烘焙到页面的HTML中。不过,您不需要为此使用硒-BeautifulSoup已经足够好了:
CSV输出:
真正的解决方案可能包括通过BeautifulSoup将对REST API的请求和对表数据的抓取结合起来——您将遍历每个国家、每个国家的竞争对手以及每年的竞争对手。我发布的更新代码假设我们只对ID
GB1
(在英国)的竞争感兴趣,并且只对2019年感兴趣编辑-您必须稍微调整我的解决方案。我只过滤并保留那些其类不包含子字符串“hide”的列,但事实证明其中一些列很重要(例如
age
列)这里有两个问题:
driver.switch_to.default_content()
以切换回iframe
shadow root
的内部。我所知道的识别这样一个元素的唯一方法是一种黑客行为,它涉及到执行javascript来获取影子根,然后在影子根中找到元素。如果我使用此代码,则可以单击该元素:相关问题 更多 >
编程相关推荐