如何使用硒执行网页上的所有JavaScript内容以在完全加载的网页上查找并发送登录表单信息 [英] How to execute all javascript content on webpage with selenium to find and send login form info on fully loaded webpage
问题描述
我一直试图制作一个Python脚本来登录某个网站,浏览菜单,填写表格并将其生成的文件保存到文件夹中.
I've been trying to make a Python script to login into a certain website, navigate through the menu, fill out a form and save the file it generates to a folder.
我一直在使用Selenium试图使网站完全加载,以便我可以找到登录元素,但是我没有成功,也许是因为网站在完全加载之前已经做了很多JavaScript内容,但是我无法使其完全加载并向我显示我想要的数据.
I've been using Selenium trying to make the website fully load so i can find the elements for the login, but i'm being unsucessful, maybe because the website does a lot of JavaScript content before it fully loads, but i can't make it fully load and show me the data i want.
我尝试了Robobrowser,Selenium,Requests和BeautifulSoup来完成它.
I tried Robobrowser, Selenium, Requests and BeautifulSoup to get it done.
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
url = "https://directa.natal.rn.gov.br/"
driver = webdriver.Chrome(executable_path="C:\\webdrivers\\chromedriver.exe")
driver.get(url)
html = driver.execute_script("return document.documentElement.outerHTML")
sel_soup = BeautifulSoup(html, 'html.parser')
senha = driver.find_element_by_xpath('//*[@id="senha"]')
senha.send_keys("123")
我希望在密码(senha)字段中填写"123",但我什至找不到该元素.
I expected to have filled the password (senha) field with "123" but i can't even find the element.
推荐答案
要将字符序列 123 发送到 password(senha)字段,作为所需的元素在<frame>
之内,因此您必须:
To send the character sequence 123 to the password (senha) field, as the the desired elements are within a <frame>
so you have to:
- 诱导 WebDriverWait 以使所需的框架可用并切换到.
- 诱导 WebDriverWait 以使所需的元素可点击.
-
您可以使用以下解决方案:
- Induce WebDriverWait for the desired frame to be available and switch to it.
- Induce WebDriverWait for the desired element to be clickable.
You can use the following solution:
-
代码块:
Code Block:
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
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument('disable-infobars')
options.add_argument("--disable-extensions")
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get("https://directa.natal.rn.gov.br/")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"frame[name='mainsystem'][src^='main']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.input[name='usuario']"))).send_keys("Tads")
driver.find_element_by_css_selector("input.input[name='senha']").send_keys("123")
浏览器快照:
这篇关于如何使用硒执行网页上的所有JavaScript内容以在完全加载的网页上查找并发送登录表单信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!