单击使用python硒的部分div链接

时间:2019-03-12 11:00:18

标签: javascript python selenium selenium-webdriver

我想在以下网站上抓取一些信息:https://www.caixabank.es/particular/prestamos-personales/prestamos.html

我编码后进入了仿真页面,但是链接使我崩溃了。

dynamic link

我无法点击此链接。

我的目标网页是那个

landing page

我的代码:

from selenium import webdriver
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=5000x2500')
webdriver = webdriver.Chrome('chromedriver',chrome_options=chrome_options)

now = DT.now()
periode = str(now.day) + '_' + str(now.month) + '_' + str(now.year)
#url = "https://www.caixabank.es/particular/prestamos-personales/prestamos.html"
url = "https://loc2.caixabank.es/GPeticiones?CANAL=I&DEMO=0&PN=LGN&PE=116&ENTORNO=1&sinCampanya=S&INICIAL_PN=PPP&INICIAL_PE=832&E_COD_LANDING=WGN00645&E_PNMENU=POS&E_PEMENU=6&TEXTO_OPE=prestamoestrellaautoes.txt&IDIOMA=02"
webdriver.get(url)
link = webdriver.find_element_by_partial_link_text('Demostración')
actions = ActionChains(webdriver).click(link)
actions.perform()
time.sleep(5)
webdriver.save_screenshot('sreenshot.png')
webdriver.implicitly_wait(30)
webdriver.switch_to.frame(webdriver.find_element_by_css_selector('#FramesetInferiorInterno > frame:nth-child(2)'))
webdriver.find_element_by_xpath("//a[contains(., 'Aceptar y continuar')]")
webdriver.save_screenshot('sreenshot1.png')

我得到的错误是:

/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:8: DeprecationWarning: use options instead of chrome_options


---------------------------------------------------------------------------

NoSuchElementException                    Traceback (most recent call last)

<ipython-input-81-858e6e0c3de3> in <module>()
     29 webdriver.save_screenshot('sreenshot.png')
     30 webdriver.implicitly_wait(30)
---> 31 webdriver.switch_to.frame(webdriver.find_element_by_css_selector('#FramesetInferiorInterno > frame:nth-child(2)'))
     32 #webdriver.switch_to.frame('Cos')
     33 #link = webdriver.find_element_by_partial_link_text('Aceptar y continuar')

/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py in find_element_by_css_selector(self, css_selector)
    596             element = driver.find_element_by_css_selector('#foo')
    597         """
--> 598         return self.find_element(by=By.CSS_SELECTOR, value=css_selector)
    599 
    600     def find_elements_by_css_selector(self, css_selector):

/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":"css selector","selector":"#FramesetInferiorInterno > frame:nth-child(2)"}
  (Session info: headless chrome=72.0.3626.121)
  (Driver info: chromedriver=72.0.3626.121,platform=Linux 4.14.79+ x86_64)

1 个答案:

答案 0 :(得分:2)

我认为您可能只需要用括号括住xpath而不是括号:

webdriver.find_element_by_xpath("//a[contains(., 'Aceptar y continuar')]")

那一个可行吗?