[英]Python Selenium - Login Page Not Appearing
我正在尝试创建执行以下操作的 Python 脚本:
(为了匿名,假设这是我的登录信息: username = 'fake_username' password = 'fake_password' )
我可以很好地完成第 1-4 步,但我在第 5 步中遇到了困难。当您在第 4 步中单击“接受”按钮时,您访问的页面 go 是空白的,并且不包括登录菜单,因此无法输入我的用户名和密码。 关于如何访问登录菜单的任何想法?
到目前为止,这是我的 Python 代码:
from selenium import webdriver
from selenium.webdriver.support.ui import Select
driver = webdriver.Chrome('chromedriver')
driver.get("https://li-public.fmcsa.dot.gov/LIVIEW/pkg_html.prc_limain")
select = Select(driver.find_element_by_id('menu'))
select.select_by_visible_text('Insurance Filing')
driver.find_element_by_xpath("//input[@src='/lihtml/go.gif' and @type='image']").click()
driver.find_element_by_xpath("//input[@type='submit' and @value=' Accept ']").click()
print(driver.page_source)
Output:
请注意,output 是一个空白的 html 页面,无法访问用于填写用户名和密码的登录菜单。 有没有办法解决这个问题?
这是基于对 PDHide 代码的轻微修改的最完整答案(在完成他的代码之后,您必须重新导航到给定的网站,因为它会自动将您转回到登录页面):
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
import time
from selenium import webdriver
from selenium.webdriver.support.ui import Select
driver = webdriver.Chrome('chromedriver')
driver.get("https://li-public.fmcsa.dot.gov/LIVIEW/pkg_html.prc_limain")
select = Select(driver.find_element_by_id('menu'))
select.select_by_visible_text('Insurance Filing')
driver.find_element_by_xpath("//input[@src='/lihtml/go.gif' and @type='image']").click()
driver.find_element_by_xpath("//input[@type='submit' and @value=' Accept ']").click()
url=driver.current_url
url = "https://put_username_here:put_password_here"+url.split("https://")[1]
driver.get(url)
#Renavigating to the page again having already logged in
select = Select(driver.find_element_by_id('menu'))
select.select_by_visible_text('Insurance Filing')
driver.find_element_by_xpath("//input[@src='/lihtml/go.gif' and @type='image']").click()
print(driver.page_source)
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
import time
from selenium import webdriver
from selenium.webdriver.support.ui import Select
driver = webdriver.Chrome('chromedriver')
driver.get("https://li-public.fmcsa.dot.gov/LIVIEW/pkg_html.prc_limain")
select = Select(driver.find_element_by_id('menu'))
select.select_by_visible_text('Insurance Filing')
driver.find_element_by_xpath("//input[@src='/lihtml/go.gif' and @type='image']").click()
driver.find_element_by_xpath("//input[@type='submit' and @value=' Accept ']").click()
url=driver.current_url
url = "https://username:password@"+url.split("https://")[1]
driver.get(url)
试试这个代码,我们得到当前的 url 并使用硬编码的用户名密码打开 url,
由于通知是浏览器级别的,因此您无法使用 selenium 处理它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.