简体   繁体   English

Selenium web 抓取:如何在响应式网站上查找元素

[英]Selenium web scraping: How to find an element on a reactive website

I am on a project that needs to get some information from a website and i wanted to automnatizate this process, but i am a begginer with the technology to achive this.我正在进行一个需要从网站获取一些信息的项目,我想自动化这个过程,但我是一个拥有实现这一目标的技术的初学者。

I found this library(Selenium) for python on the internet and i thougth it maight be a solution.我在互联网上找到了 python 的这个库(Selenium),我认为它可能是一个解决方案。

I succeded the firsts steps (Accepting cookies, locating the "Access" button and clicking on it).我成功了第一步(接受 cookies,找到“访问”按钮并单击它)。 the problem comes when the "Access" button is clicked.单击“访问”按钮时出现问题。 It shows a little form to input the user and password, but i couldn't found them using the driver.find_... methods, so i began to look for the elements on the html document and it seems that this form is injected after clicking on the "Accept" button.它显示了一个输入用户和密码的小表单,但我无法使用driver.find_...方法找到它们,所以我开始在 html 文档上查找元素,似乎这个表单是在之后注入的点击“接受”按钮。

Is there any strategy to find the input elements for introducing the user and password after clicking the button?有没有什么策略可以在点击按钮后找到用于介绍用户和密码的输入元素?

HTML code of the website without having clicked de "Access" button网站的 HTML 代码,无需单击“访问”按钮

HTML code after clicking on the "Access" button HTML代码点击“访问”按钮后

Thank you谢谢

The website link is: https://www.bbva.es/personas/experiencias/bbva-valora/analiza-vivienda.html网站链接为: https://www.bbva.es/personas/experiencias/bbva-valora/analiza-vivienda.html

<iframe class="iframe__base" title="People Login" id="tab-personas-iframe" width="100%" style="max-width: 100%; overflow: hidden; height: 384px;" src="/nimbus/login.html?conf=net/login&amp;http_contactid=04a341b5-c0ce-454a-8854-be6b12299c9a" scrolling="no">Your browser does not support iframes</iframe>

Your elements are inside an iframe.您的元素位于 iframe 中。

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC


wait=WebDriverWait(driver, 40)
driver.get('https://www.bbva.es/personas/experiencias/bbva-valora/analiza-vivienda.html')
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,".cookiesgdpr__acceptbtn.btn__basic.btn__medium-blue"))).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,".header__actions__item__link.header__actions--menu.header__access"))).click()

So here you have to switch to the iframe and then find element所以这里你必须切换到 iframe 然后找到元素

wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID,"tab-personas-iframe")))
wait.until(EC.element_to_be_clickable((By.XPATH,"//input[@data-testid='login-form-user']"))).send_keys("USER")
wait.until(EC.element_to_be_clickable((By.XPATH,"//input[@data-testid='login-form-password']"))).send_keys("PASSWORD")
wait.until(EC.element_to_be_clickable((By.XPATH,"//input[@data-testid='login-form-submit']"))).click()

To get where you want after you click the accept popup and the acceso item you just switch frames and send key.要在单击接受弹出窗口和访问项后到达您想要的位置,您只需切换帧并发送密钥。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM