编辑的帖子
首先,有一个类似的问题在现场,但它没有工作,这就是为什么我要问。我应该决定在HTML代码上是否有一个特定的文本LİSTELENECEK VERİ BULUNAMAMIŞTIR.,只有id属性在这里是HTML:
<center id="genelUyariCenterTag">LİSTELENECEK VERİ BULUNAMAMIŞTIR.</center>以下是C#代码:
WebClient client = new WebClient();
void goMadde15Down() {
driver.FindElement(By.LinkText("İŞVEREN")).Click();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
driver.FindElement(By.LinkText("TEŞVİKLER VE TANIMLAR")).Click();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
driver.FindElement(By.LinkText("4447/GEÇİCİ 15. MADDE LİSTELEME/SİLME")).Click();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
string siteSource15 = client.DownloadString("https://uyg.sgk.gov.tr/IsverenSistemi");
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");
}问题是,条件总是收敛于else子句。
发布于 2020-02-04 14:35:24
与if和else不同,如果可以使用try-catch块,则可以使用以下代码:
void goMadde15Down() {
driver.FindElement(By.LinkText("İŞVEREN")).Click();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
driver.FindElement(By.LinkText("TEŞVİKLER VE TANIMLAR")).Click();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
driver.FindElement(By.LinkText("4447/GEÇİCİ 15. MADDE LİSTELEME/SİLME")).Click();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
try {
Console.WriteLine("There is an Excel File");
//Download code here
}
catch (Exception e)
{
Console.WriteLine("There is no Excel File");
}另外,try catch块比if和else更有效。
发布于 2020-02-04 12:53:08
归纳WebDriverWait和ElementExists并在xpath下面使用。
您需要从SeleniumExtras.WaitHelpers安装Manage Nuget Packages包。
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");
}https://stackoverflow.com/questions/60056560
复制相似问题