我目前正在嘗試在完成表格后從自動化測驗網站獲取一些列印文本。提交表單后,回傳的值會在螢屏上顯示給用戶,但由于某種原因,我無法使用 Selenium 的 WebElement 獲取 4 個文本值中的 2 個,我嘗試使用.Textand GetAttribute("value"),但它們都回傳空白。然而,回傳的前 2 段文本,我能夠檢索。請參閱下面的螢屏截圖以及代碼。

//然后我驗證提交的表單
Constants.confirmationName = driver.FindElement(By.CssSelector("#name"));
if (Constants.confirmationName.Text == "Name:QA Automation")
{
Console.WriteLine(Constants.confirmationName.Text);
}
else
{
Console.WriteLine("We have a different name stored for you.");
}
Constants.confirmationEmail = driver.FindElement(By.CssSelector("#email"));
if (Constants.confirmationEmail.Text == "Email:[email protected]")
{
Console.WriteLine(Constants.confirmationEmail.Text);
}
else
{
Console.WriteLine("We have a different email stored for you.");
}
Thread.Sleep(2000);
//NOT WORKING
Constants.confirmationCurrentAddress = driver.FindElement(By.CssSelector("#currentAddress"));
Thread.Sleep(2000);
if (Constants.confirmationCurrentAddress.Text == "Current Address :Cedars 2 ")
{
Console.WriteLine(Constants.confirmationCurrentAddress.Text);
}
else
{
Console.WriteLine("We have a different current address stored for you.");
}
//NOT WORKING
Constants.confirmationPermanentAddress = driver.FindElement(By.CssSelector("#permanentAddress"));
if (Constants.confirmationPermanentAddress.Text == "Permananet Address :63 Wheat Drive")
{
Console.WriteLine(Constants.confirmationPermanentAddress.Text);
}
else
{
Console.WriteLine("We have a different permanent address stored for you.");
}
代碼確認列印的名稱,我可以看到它回傳,電子郵件地址也是如此,但當前地址和永久地址都回傳空白,我也嘗試添加等待時間,但無濟于事。
有問題的網站是
您可以使用
因此,要提取Current Address和Permananet Address的值而不是使用Text屬性,您需要使用GetAttribute("value")方法,如下所示:
提取當前地址:
Constants.confirmationCurrentAddress = driver.FindElement(By.CssSelector("#currentAddress")); if (Constants.confirmationCurrentAddress.GetAttribute("value") == "Current Address :Cedars 2 ") { Console.WriteLine(Constants.confirmationCurrentAddress.Text); }提取永久地址:
Constants.confirmationPermanentAddress = driver.FindElement(By.CssSelector("#permanentAddress")); if (Constants.confirmationPermanentAddress.GetAttribute("value") == "Permananet Address :63 Wheat Drive") { Console.WriteLine(Constants.confirmationPermanentAddress.Text); }
uj5u.com熱心網友回復:
您應該等待 DOM 加載。如果元素以某種方式隱藏,您可以獲得該innerHTML屬性。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/366537.html
