未找到元素C#

未找到元素C#,c#,selenium,selenium-webdriver,webdriver,phantomjs,C#,Selenium,Selenium Webdriver,Webdriver,Phantomjs,网址: 我正在尝试从上面的客户端站点自动下载手稿。我在C#中使用selenium phantomjs 我有用户凭据。但是组成登录表单的元素(例如用户名、密码)在页面源中不存在,但在浏览器中检查这些元素时,这些元素就存在了 这些是我用来从“Inspect元素”定位它们的XPath(ID是动态分配的,这就是为什么我没有使用它们): 如果驱动程序返回的源没有登录元素,因此无法找到,但在浏览器中检查它们时仍然存在,那么如何成功登录 代码: 使用系统; 使用System.Collections.Gener

网址:

我正在尝试从上面的客户端站点自动下载手稿。我在C#中使用selenium phantomjs

我有用户凭据。但是组成登录表单的元素(例如用户名、密码)在页面源中不存在,但在浏览器中检查这些元素时,这些元素就存在了

这些是我用来从“Inspect元素”定位它们的XPath(ID是动态分配的,这就是为什么我没有使用它们):

如果驱动程序返回的源没有登录元素,因此无法找到,但在浏览器中检查它们时仍然存在,那么如何成功登录

代码:

使用系统;
使用System.Collections.Generic;
使用System.Linq;
运用系统反思;
使用System.IO;
使用OpenQA.Selenium;
使用OpenQA.Selenium.PhantomJS;
使用OpenQA.Selenium.Support.UI;
使用WebCrawler.Helper;
名称空间WebCrawler.Webcodes
{
类RSuite:IWebcode
{
列表错误=新列表();
字符串xpathUsername=“.//输入[@name='user']”;
字符串xpathPassword=“../input[@name='pass']”;
字符串xpathBtnLogin=“.//按钮[@type='submit'][@aria label='Log In']”;
公共资源套件()
{ }
公共列表记录()
{
WriteLine(string.Format(“爬行:{0}”,Config.Url));
列表记录列表=新列表();
尝试
{
PhantomJSDriverService服务=PhantomJSDriverService.CreateDefaultService();
service.HideCommandPromptWindow=true;
使用(IWebDriver=newPhantomJSDriver(服务))
{
driver.Navigate().gotour(Config.Url);
Console.WriteLine(\n检查元素可用性…);
//这里的代码异常:我无法获取所有这些元素
IWebElement用户名=元素(“用户ID”,GetElement(驱动程序,xpathUsername));
IWebElement password=Element(“密码”,GetElement(驱动程序,xpathPassword));
IWebElement btnlogin=Element(“登录按钮”,GetElement(驱动程序,xpathBtnLogin));
//输入凭证
Console.WriteLine(“\n尝试登录…”);
如果(用户名!=null&&password!=null&&btnlogin!=null)
{
username.Clear();
username.SendKeys(Config.username);
password.Clear();
password.SendKeys(Config.password);
//是否单击按钮并加载新页面?(如果为true,则登录成功)
如果(IsPageLoaded(驱动程序、btnlogin))
{
Console.WriteLine(“成功登录”);
//采取行动
//下载文件
}
其他的
{ErrorHandler(“登录失败”);}
}
其他的
{ErrorHandler(“登录失败”);}
}
//释放
service.Dispose();
}
捕获(异常错误)
{ErrorHandler(err.GetBaseException().ToString());}
//为捕获的错误(如果有)生成报告
if(errors.Count()>0)
Config.ErrorReport(this.GetType().Name.Trim().ToUpper(),string.Join(“\n\n”,errors));
返回记录列表;
}
私有IWebElement GetElement(IWebDriver驱动程序,字符串xPath)
{
IWebElement元素=null;
尝试
{
//等待加载元素
WebDriverWait wait=新的WebDriverWait(驱动程序,TimeSpan.FromSeconds(60));
Func waitForElement=新函数((IWebDriver d)=>
{
element=d.FindElement(By.XPath(XPath));
if(元素!=null)
{返回元素;}
返回null;
});
返回wait.Until(waitForElement);
}
捕获(异常错误)
{
ErrorHandler(err.GetBaseException().ToString());
返回null;
}
}
私有IWebElement元素(字符串标签,IWebElement元素)
{
if(元素!=null)
{Console.WriteLine(string.Format({0}:Yes,label));}
其他的
{Console.WriteLine(string.Format(“{0}:No”,label));}
返回元素;
}
私有bool显示加载(IWebDriver驱动程序,IWebElement元素)
{
尝试
{
//页面重定向?或者元素仍然附加到DOM?
WebDriverWait wait=新的WebDriverWait(驱动程序,TimeSpan.FromSeconds(60));
元素。单击();
返回wait.till(ExpectedConditions.StalenessOf(元素));
}
捕获(异常错误)
{
ErrorHandler(err.GetBaseException().ToString());
返回false;
}
}
私有void ErrorHandler(字符串错误)
{
控制台写入线(错误);
错误。添加(错误);
}
}
}
根据您的问题,url
http://bcmprod.brill.com/rsuite-cms/
是基于的,因此在查找元素时,必须将WebDriverWait与ExpectedConditions方法相结合

识别所需文件的方法如下:

  • 用户ID

    string username = "//input[@class='ember-view ember-text-field finput text-field component' and @name='user']";
    
  • 密码

    string password = "//input[@class='ember-view ember-text-field finput text-field component' and @name='pass']";
    
  • 登录

    string loginbtn = "//label[@class='ui-button-text']";
    

尝试按F12在浏览器控制台中查看实际的页面源代码。我将尝试元素stobeclickable()。@poy它是
元素tobeclickab
string password = "//input[@class='ember-view ember-text-field finput text-field component' and @name='pass']";
string loginbtn = "//label[@class='ui-button-text']";