Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我如何确定网站中是否有特定的文本_C#_Visual Studio_Selenium_Selenium Webdriver_Webclient - Fatal编程技术网

C# 我如何确定网站中是否有特定的文本

C# 我如何确定网站中是否有特定的文本,c#,visual-studio,selenium,selenium-webdriver,webclient,C#,Visual Studio,Selenium,Selenium Webdriver,Webclient,编辑的帖子 首先,有一个类似的问题在网站上,但它没有工作,这就是为什么我问。 我应该决定是否有一个特定的文本LİSTELENECEK VERİbulunamiŞTIR.在HTML代码上,只有id属性这里是HTML代码: LİSTELENECEK VERİbulunamiŞTIR。 以下是C#代码: WebClient=newWebClient(); void gomad15down(){ driver.FindElement(By.LinkText(“İŞVEREN”))。单击(); driv

编辑的帖子

首先,有一个类似的问题在网站上,但它没有工作,这就是为什么我问。 我应该决定是否有一个特定的文本
LİSTELENECEK VERİbulunamiŞTIR.
在HTML代码上,只有
id
属性这里是HTML代码:

LİSTELENECEK VERİbulunamiŞTIR。
以下是C#代码:

WebClient=newWebClient();
void gomad15down(){
driver.FindElement(By.LinkText(“İŞVEREN”))。单击();
driver.Manage().Timeouts().ImplicitWait=TimeSpan.FromSeconds(10);
driver.FindElement(By.LinkText(“TEŞVİKLER VE TANIMLAR”))。单击();
driver.Manage().Timeouts().ImplicitWait=TimeSpan.FromSeconds(10);
driver.FindElement(By.LinkText(“4447/GEİCİ15.MADDE LİSTELEME/SİLME”)。单击();
driver.Manage().Timeouts().ImplicitWait=TimeSpan.FromSeconds(10);
string siteSource15=客户端。下载字符串(“https://uyg.sgk.gov.tr/IsverenSistemi");
字符串strText=driver.findelelement(By.XPath(//center[@id='genelUyariCenterTag'并包含(,'bulunamiŞTIR')))))))。文本;
if(标准文本包含(“LİSTELENECEK VERİbulumanamiŞTIR.”)
{
Console.WriteLine(“madde15没有Excel”);
}
其他的
{
Console.WriteLine(“madde 15有一个Excel”);
}

问题是,条件总是收敛到
else
子句

归纳
WebDriverWait
ElementExists
并在xpath下面使用

您需要从管理Nuget软件包安装SeleniumExtras.WaitHelpers

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
string strText=wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(By.XPath("//center[@id='genelUyariCenterTag' and contains(.,'BULUNAMAMIŞTIR')]"))).Text;
    if (strText.Contains("LİSTELENECEK VERİ BULUNAMAMIŞTIR."))
            {
                Console.WriteLine("madde15 there is no Excel");
            }
     else
            {
                Console.WriteLine("madde 15 there is an Excel");
             }

尝试不使用
WebDriverWait

string strText=driver.FindElement(By.XPath("//center[@id='genelUyariCenterTag' and contains(.,'BULUNAMAMIŞTIR')]")).Text;

            if (strText.Contains("LİSTELENECEK VERİ BULUNAMAMIŞTIR."))
                {
                Console.WriteLine("madde15 there is no Excel");
                }
                else
                {
                Console.WriteLine("madde 15 there is an Excel");
                }

如果可以使用
try catch
块,则可以使用以下代码,而不是
if
else

void goMadde15Down(){
driver.FindElement(By.LinkText(“İŞVEREN”))。单击();
driver.Manage().Timeouts().ImplicitWait=TimeSpan.FromSeconds(10);
driver.FindElement(By.LinkText(“TEŞVİKLER VE TANIMLAR”))。单击();
driver.Manage().Timeouts().ImplicitWait=TimeSpan.FromSeconds(10);
driver.FindElement(By.LinkText(“4447/GEİCİ15.MADDE LİSTELEME/SİLME”)。单击();
driver.Manage().Timeouts().ImplicitWait=TimeSpan.FromSeconds(10);
试一试{
Console.WriteLine(“有一个Excel文件”);
//在这里下载代码
}
捕获(例外e)
{
Console.WriteLine(“没有Excel文件”);
}

另外,
try-catch
block比
if
else

更有效,为什么不使用selenium
webdriver
来验证
webelement
的文本?你有没有尝试过类似的方法?实际上我是Visual Studio和C的初学者,你能举个例子吗?从现在开始,我点击按钮或f使用Selenium从数据库中下载表单。您好,@hasanhüseyin我了解您的HTML免费文本,我们使用的是同一种语言。您是否正在尝试下载excel文件(如果存在)或者转到下一页?是的,这正是我想做的。感谢您的努力,但当我尝试安装
SeleniumExtras.WaitHelpers
无法解析依赖项一样警告我。“Selenium.WebDriver 3.141.0”与“SeleniumExtras.WaitHelpers 1.0.0”约束不兼容:Selenium.WebDriver(>=4.0.0-alpha04)“.
当我检查版本更新时,我使用的是3.141.0版本。3.141.0已经是最新版本。@hasanhüseyin:Try not
WebDriverWait
检查我的答案。现在打开
string strText=driver.FindElement(By.XPath(//center[@id='geneluyaricenterag'并包含(,'bulunamiŞTIR')).Text;
I get error说
OpenQA.Selenium.WebDriverException:“无法将数据转换为对象”
您能在原始帖子上发布更新后的代码以检查哪里出错了吗?这应该行得通。@hasanhüseyin:您能提到到达该文本的步骤吗?我找不到何时可以看到url,我相信您已经定义了web驱动程序使用浏览器chrome或FF或IE。