无法找到元素Selenium WebDriver
我正在尝试抓取以下网站:https :
//www.bancosantander.es/es/particulares/prestamos/prestamo-
coche/simulador
我正在尝试做的是:
通过移动滑块来模拟数量和持续时间。我正在尝试使用网络驱动程序找到它们。
但是我不能,事实上我已经开始抓取了。
出现的错误如下:
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:12: DeprecationWarning: use options instead of chrome_options if sys.path[0] == '':
---------------------------------------------------------------------------
NoSuchElementException Traceback (most recent call last)
<ipython-input-29-f71d54453a68> in <module>()
20 pass
21 webdriver.save_screenshot('sreenshot0.png')
---> 22 webdriver.find_element_by_xpath("//*[@class='res r1']")
23
24 webdriver.save_screenshot('sreenshot0.png')
/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py in find_element_by_xpath(self, xpath)
392 element = driver.find_element_by_xpath('//div/td[1]')
393 """
--> 394 return self.find_element(by=By.XPATH, value=xpath)
395
396 def find_elements_by_xpath(self, xpath):
/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py in find_element(self, by, value)
976 return self.execute(Command.FIND_ELEMENT, {
977 'using': by,
--> 978 'value': value})['value']
979
980 def find_elements(self, by=By.ID, value=None):
/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py in execute(self, driver_command, params)
319 response = self.command_executor.execute(driver_command, params)
320 if response:
--> 321 self.error_handler.check_response(response)
322 response['value'] = self._unwrap_value(
323 response.get('value', None))
/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
240 alert_text = value['alert'].get('text')
241 raise exception_class(message, screen, stacktrace, alert_text)
--> 242 raise exception_class(message, screen, stacktrace)
243
244 def _value_or_default(self, obj, key, default):
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@class='res r1']"}
(Session info: headless chrome=72.0.3626.121)
(Driver info: chromedriver=72.0.3626.121,platform=Linux 4.14.79+ x86_64)
我的代码:
from selenium import webdriverfrom selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--start-maximized')
chrome_options.add_argument('window-size=4400x2200')
webdriver = webdriver.Chrome('chromedriver',chrome_options=chrome_options)
webdriver.maximize_window()
url = "https://www.bancosantander.es/es/particulares/prestamos/prestamo-coche/simulador"
webdriver.get(url)
try:
webdriver.find_element_by_xpath("//button[@onclick=\"_W034_Cookie_Directive_WAR_W034_Cookie_Directiveportlet_.hidePortlet()\"]").click()
except NoSuchElementException:
pass
webdriver.save_screenshot('sreenshot0.png')
webdriver.find_element_by_xpath("//*[@class='res r1']")
webdriver.save_screenshot('sreenshot0.png')
回答:
您可以尝试以下方法输入值
from selenium import webdriverfrom selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
url = 'https://simuladores.bancosantander.es/SantanderES/loansimulatorweb.aspx?por=webpublica&prv=publico&m=100&cta=1&ls=0#/t0'
driver = webdriver.Chrome()
driver.get(url)
amount = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "#amount")))
amount.clear()
amount.send_keys(6000)
WebDriverWait(driver, 30).until(lambda driver: driver.execute_script('return jQuery.active') == 0)
term = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "#term")))
term.clear()
term.send_keys('60 meses')
term.send_keys(Keys.TAB)
currentCuota = driver.find_element_by_css_selector('.r1 span').text
WebDriverWait(driver, 10).until(lambda driver: driver.execute_script('return jQuery.active') == 0)
while driver.find_element_by_css_selector('.r1 span').text == currentCuota:
continue
print('Tu cuota mensual: ' + driver.find_element_by_css_selector('.r1 span').text)
以上是 无法找到元素Selenium WebDriver 的全部内容, 来源链接: utcz.com/qa/401021.html