So I am trying to access the website and I want the program to apply some filters for me via click. Although when I try to use the function find_element_by_xpath it says that this function is depreciated. Can someone help me ? Also the website is kind of weird because it is more like a dashboard than a website and so I dont know how precise xpath will work. thank you all so much in advance
%pip install selenium webdriver_manager
import requests
from datetime import datetime
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
url_dist_vacinas =
'https://infoms.saude.gov.br/extensions/DEMAS_C19VAC_Distr/DEMAS_C19VAC_Distr.html'
driver = webdriver.Chrome()
driver.get(url_dist_vacinas)
driver.find_element_by_xpath('//*[@id="filtro-04"]/div/article/div[1]/div/div/qv-
filterpane/div/div/div/div[2]/span').click()
ERROR MESSAGE:
C:\Users\gbranco\AppData\Local\Temp\ipykernel_34036\402499948.py:2:
DeprecationWarning: find_element_by_xpath is deprecated. Please use find_element(by=By.XPATH,
value=xpath) instead
driver.find_element_by_xpath('//*[@id="filtro-04"]/div/article/div[1]/div/div/qv-
filterpane/div/div/div/div[2]/span').click()
---------------------------------------------------------------------------
NoSuchElementException Traceback (most recent call last)
Input In [9], in <cell line: 2>()
1 #Auf jeweilige Felder klicken
----> 2 driver.find_element_by_xpath('//*[@id="filtro-04"]/div/article/div[1]/div/div/qv-
filterpane/div/div/div/div[2]/span').click()
File ~\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py:526, in
WebDriver.find_element_by_xpath(self, xpath)
504 """
505 Finds an element by xpath.
506
(...)
519 element = driver.find_element_by_xpath('//div/td[1]')
520 """
521 warnings.warn(
522 "find_element_by_xpath is deprecated. Please use find_element(by=By.XPATH,
value=xpath) instead",
523 DeprecationWarning,
524 stacklevel=2,
525 )
--> 526 return self.find_element(by=By.XPATH, value=xpath)
File ~\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py:1251, in
WebDriver.find_element(self, by, value)
1248 by = By.CSS_SELECTOR
1249 value = '[name="%s"]' % value
-> 1251 return self.execute(Command.FIND_ELEMENT, {
1252 'using': by,
1253 'value': value})['value']
File ~\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py:430, in
WebDriver.execute(self, driver_command, params)
428 response = self.command_executor.execute(driver_command, params)
429 if response:
--> 430 self.error_handler.check_response(response)
431 response['value'] = self._unwrap_value(
432 response.get('value', None))
433 return response
File ~\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py:247, in
ErrorHandler.check_response(self, response)
245 alert_text = value['alert'].get('text')
246 raise exception_class(message, screen, stacktrace, alert_text) # type: ignore[call-
arg] # mypy is not smart enough here
--> 247 raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: no such element: Unable to locate element:
{"method":"xpath","selector":"//*[@id="filtro-04"]/div/article/div[1]/div/div/qv-
filterpane/div/div/div/div[2]/span"}
(Session info: chrome=102.0.5005.63)
Stacktrace:
Backtrace:
Ordinal0 [0x00A7D953 2414931]
Ordinal0 [0x00A0F5E1 1963489]
Ordinal0 [0x008FC6B8 837304]
Ordinal0 [0x00929500 1021184]
Ordinal0 [0x0092979B 1021851]
Ordinal0 [0x00956502 1205506]
Ordinal0 [0x009444E4 1131748]
Ordinal0 [0x00954812 1198098]
Ordinal0 [0x009442B6 1131190]
Ordinal0 [0x0091E860 976992]
Ordinal0 [0x0091F756 980822]
GetHandleVerifier [0x00CECC62 2510274]
GetHandleVerifier [0x00CDF760 2455744]
GetHandleVerifier [0x00B0EABA 551962]
GetHandleVerifier [0x00B0D916 547446]
Ordinal0 [0x00A15F3B 1990459]
Ordinal0 [0x00A1A898 2009240]
Ordinal0 [0x00A1A985 2009477]
Ordinal0 [0x00A23AD1 2046673]
BaseThreadInitThunk [0x762AFA29 25]
RtlGetAppContainerNamedObjectPath [0x77037A7E 286]
RtlGetAppContainerNamedObjectPath [0x77037A4E 238]
CodePudding user response:
The Solution For this problem is :
using the new feature find_element()
include this import too
from selenium.webdriver.common.by import By
this is an example :
button = driver.find_element_by_class_name("quiz_button")
button = driver.find_element(By.CLASS_NAME, "quiz_button")
please see this selenium documentation : https://www.selenium.dev/selenium/docs/api/py/api.html
CodePudding user response:
driver.find_element_by_xpath('//*[@id="filtro-04"]/div/article/div[1]/div/div/qv-
filterpane/div/div/div/div[2]/span').click()
Due to Depreciation Warning use the updated By method
driver.find_element(By.XPATH,'//*[@id="filtro-04"]/div/article/div[1]/div/div/qv-
filterpane/div/div/div/div[2]/span').click()
From there due to page loading.
wait = WebDriverWait(driver, 30)
wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="filtro-04"]/div/article/div[1]/div/div/qv-filterpane/div/div/div/div[2]/span'))).click()
Imports:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC