首页 > 解决方案 > 登录到使用重定向的网页

问题描述

我正在尝试自动登录并稍后执行下载加载。页面上有一个重定向,一旦我尝试通过 Selenium Web 驱动程序直接访问该页面,我就会从该页面收到一条错误消息:

先前的身份验证会话已过期。为了继续,您必须重新登录。

我在首页找到了一个重定向链接,并通过此链接从 selenium 访问,但现在我收到一条错误消息

var userNameField = driver.FindElementById("loginForm:username");

在页面上找不到。

OpenQA.Selenium.NoSuchElementException:'没有这样的元素:无法找到元素:{“method”:“css selector”,“selector”:“#loginForm:username”}(会话信息:chrome = 78.0.3904.108)'

问题出在哪里?是不是我应该放弃一些“等待”,直到页面加载命令?正如您在我的代码中看到的那样,我添加了“等待”(希望它是正确的),但结果仍然是相同的错误。

这是我当前的代码:

using System.IO;
using OpenQA.Selenium.Chrome;


namespace WebDriverTest
{
    class Program
    {
        static void Main(string[] args)
        {

            // Initialize the Chrome Driver
            using (var driver = new ChromeDriver())
            {
                // Go to the home page
                driver.Navigate().GoToUrl("https://portal.vastuugroup.fi/api/general/external-redirects?lang=fi&sp_route=/");

                // Get the page elements
                OpenQA.Selenium.Support.UI.WebDriverWait wait = new OpenQA.Selenium.Support.UI.WebDriverWait(driver, System.TimeSpan.FromSeconds(20));

                var userNameField = driver.FindElementById("loginForm:username");
                var userPasswordField = driver.FindElementById("loginForm:password");
                var loginButton = driver.FindElementByXPath("//input[@id='loginForm:loginButton']");

                // Type user name and password
                userNameField.SendKeys("user@email.com");
                userPasswordField.SendKeys("12345");

                // and click the login button
                loginButton.Click();

                // Extract the text and save it into result.txt
                // var result = driver.FindElementByXPath("//div[@id='case_login']/h3").Text;
                // File.WriteAllText("result.txt", result);

                // Take a screenshot and save it into screen.png
                driver.GetScreenshot().SaveAsFile(@"screen.png", OpenQA.Selenium.ScreenshotImageFormat.Png);
            }
        }
    }
}

标签: c#seleniumselenium-webdriverweb-scrapingweb-crawler

解决方案


试试下面的代码:

using System.IO;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;

namespace WebDriverTest
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize the Chrome Driver
            using (var driver = new ChromeDriver())
            {
                // Go to the home page
                driver.Navigate().GoToUrl("https://portal.vastuugroup.fi/api/general/external-redirects?lang=fi&sp_route=/");
                   driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(15);
                // Get the page elements
                var userNameField = driver.FindElementById("loginForm:username");
                var userPasswordField = driver.FindElementById("loginForm:password");
                var loginButton = driver.FindElementById("loginForm:loginButton");

                // Type user name and password
                userNameField.SendKeys("username");
                userPasswordField.SendKeys("password");

                // and click the login button
                loginButton.Click();

            }
        }
    }
}

推荐阅读