我正在尝试从wunderground.com抓取一些数据。 由于我居住在使用公制的国家,因此我想更改设置,以便使用公制显示所有数据。
公制设置隐藏在下拉菜单中 pic of settings button我似乎无法单击,因为单击click()之后无法在后续的“下拉列表”中找到该元素。
这是我用来尝试更改设置的代码:
class WundergroundLong():
def __init__(self):
self.r ='https://www.wunderground.com/dashboard/pws/IDEKRIM2'
self.chrome_options = webdriver.ChromeOptions()
self.chrome_options.add_argument('--headless')
self.chrome_options.add_argument('--no-sandbox') # required when running as root user. otherwise you would get no sandbox errors.
self.driver = webdriver.Chrome(chrome_options=self.chrome_options)
def scrapeTable(self):
self.driver.get(self.r)
self.settings = self.driver.find_element(By.XPATH, "//button[@id='wuSettings']")
self.settings.click()
WebDriverWait(self.driver,4)
self.metric = self.driver.find_element(By.XPATH, '//*[@id="wuSettings-quick"]/div/a[2]')
self.metric.click()
instance = WundergroundLong()
table = instance.scrapeTable()
显示以下错误代码
Traceback (most recent call last):
File "/home/wouter/Programming/kantine_scripts/wunderground/wunderground.py", line 85, in <module>
table = instance.scrapeTable()
File "/home/wouter/Programming/kantine_scripts/wunderground/wunderground.py", line 37, in scrapeTable
self.metric = self.driver.find_element(By.XPATH, '//*[@id="wuSettings-quick"]/div/a[2]')
File "/home/wouter/Programming/kantine_scripts/env/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 978, in find_element
'value': value})['value']
File "/home/wouter/Programming/kantine_scripts/env/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/wouter/Programming/kantine_scripts/env/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="wuSettings-quick"]/div/a[2]"}
(Session info: headless chrome=63.0.3239.132)
(Driver info: chromedriver=2.35.528139 (47ead77cb35ad2a9a83248b292151462a66cd881),platform=Linux 4.14.109-1-MANJARO x86_64)