找到并移动一些bouton webdriver python

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (62)

我的问题是:我想找到某些元素,并填写诸如Cuántodineronecesitas和Encuántosmesesquieres devolverlo这样的价值观。我尝试了一切,但我根本做不到。

实际上,我想模拟值,并检索像TIN这样的信息。

我的网站是:https//www.bancosantander.es/es/particulares/prestamos/prestamo-coche/simulador

我的代码:

from selenium import webdriver
from 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)
url = "https://www.bancosantander.es/es/particulares/prestamos/prestamo-coche/simulador"
webdriver.get(url)
webdriver.find_element_by_xpath("//button[@onclick=\"_W034_Cookie_Directive_WAR_W034_Cookie_Directiveportlet_.hidePortlet()\"]").click()
webdriver.find_element_by_xpath("//*[@class='input ng-isolate-scope ng-valid ng-dirty']")
webdriver.save_screenshot('sreenshot0.png')

我收到以下错误:

/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-163-5625acccacee> in <module>()
     16 webdriver.get(url)
     17 webdriver.find_element_by_xpath("//button[@onclick=\"_W034_Cookie_Directive_WAR_W034_Cookie_Directiveportlet_.hidePortlet()\"]").click()
---> 18 webdriver.find_element_by_xpath("//*[@class='input ng-isolate-scope ng-valid ng-dirty']")
     19 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='input ng-isolate-scope ng-valid ng-dirty']"}
  (Session info: headless chrome=71.0.3578.98)
  (Driver info: chromedriver=71.0.3578.98,platform=Linux 4.14.79+ x86_64)
提问于
用户回答回答于

从我所看到的,这个元素并不总是存在。您可以使用try exceptblock 包装命令

try:
    webdriver.find_element_by_xpath("//button[@onclick=\"_W034_Cookie_Directive_WAR_W034_Cookie_Directiveportlet_.hidePortlet()\"]").click()
except NoSuchElementException:
    pass

或者使用find_elements并检查按钮是否存在

elements = webdriver.find_elements_by_xpath("//button[@onclick=\"_W034_Cookie_Directive_WAR_W034_Cookie_Directiveportlet_.hidePortlet()\"]")
if elements:
    elements[0].click()

您还可以在此处添加显式等待,页面加载需要一些时间

try:
    WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//button[@onclick=\"_W034_Cookie_Directive_WAR_W034_Cookie_Directiveportlet_.hidePortlet()\"]")).click()
except TimeoutException:
    pass

扫码关注云+社区

领取腾讯云代金券