工作,我想從這個結構得到文本Sample
:.TEXT不是硒PhantomJS C#
<td id="IDName">Sample</td>
所以我嘗試這樣的:
driver1.FindElement(By.Id("IDName")).Text;
,但它總是返回null
。
有什麼理由不工作?
工作,我想從這個結構得到文本Sample
:.TEXT不是硒PhantomJS C#
<td id="IDName">Sample</td>
所以我嘗試這樣的:
driver1.FindElement(By.Id("IDName")).Text;
,但它總是返回null
。
有什麼理由不工作?
這很難說,爲什麼.Text
是不是在你的情況下工作,可能是可能的,當你會發現元素是存在於DOM
沒有文字,所以你應該WebDriverWait
等到元素如下存在: -
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement element = wait.Until(ExpectedConditions.ElementExists(By.Id("IDName")));
element.Text;
或者也許是可能它的設計問題,你的HTML,那麼你可以通過如下使用.GetAttribute("innerHTML")
獲取文本: -
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement element = wait.Until(ExpectedConditions.ElementExists(By.Id("IDName")));
element.GetAttribute("innerHTML");
或者,你可以通過使用獲取文本如下: -
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement element = wait.Until(ExpectedConditions.ElementExists(By.Id("IDName")));
element.GetAttribute("textContent");
希望它能幫助... :)
看看它是否下架的HTML DOM –