问题
enter image description here
Please provide me the scripting code in C# to get the text of the element that are marked in blue box. I am using Selenium Web driver and I want to get the value:10975 in the td tag.
I tried the following code:
IWebElement tableLocator = divLocator.FindElement(table);
IWebElement tbodyFind = tableLocator.FindElement(tbodytag);
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.ElementIsVisible(trTag));
List<IWebElement> trList = tbodyFind.FindElements(trTag).ToList();
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.ElementIsVisible(tdTag));
//List<IWebElement> tdList = trList[1].FindElements(tdTag).ToList();
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
wait.Until(ExpectedConditions.ElementExists(By.XPath("/html/body/div[2]/section/form/div[2]/div/div/table/tbody/tr[1]/td[3]")));
IWebElement hiddenElem = trList[0].FindElement(By.XPath("/html/body/div[2]/section/form/div[2]/div/div/table/tbody/tr[1]/td[3]"));
回答1:
Selenium
simulates user actions. If the element is not visible and the user can't read it, Selenium
can't read it as well. To avoid this problem we can use the WebElement
attributes.
IList<IWebElement> hiddenElements = driver.FindElements(By.CssSelector(".k-master-row > [style=display:none]"));
string text = hiddenElements[1].GetAttribute("textContent");
You should have the two hidden elements in the list. Just access the second by index and get the text.
来源:https://stackoverflow.com/questions/35008101/how-can-i-get-displaynone-element-using-selenium-webdriver-c