【发布时间】:2019-10-28 11:35:50
【问题描述】:
所以我相信我能够通过 CSS Selector 或 Xpath 甚至通过 TagName 来获取元素,但我无法获取与其关联的文本。
这是 HTML 的样子:
<label for="AnswerText">This is the question text? </label>
在我的代码中我有这个:
static IWebDriver driver = new ChromeDriver(".");
static IWebElement securityQuestion = driver.FindElement(By.TagName("label"));
Console.WriteLine("question is ", securityQuestion.Text)
我想要做的是从标签“这是问题文本?”中获取此文本。我这样做是因为文本会发生变化,并且根据答案,输入会有所不同,因此我必须合并逻辑才能获取该文本并将其设置为等于要匹配的变量。
我尝试过 .Text GetAttribute("value") 但这不起作用,因为标签没有任何值以及不同的选择器。
static IWebDriver driver = new ChromeDriver(".");
static IWebElement securityQuestion = driver.FindElement(By.TagName("label"));
Console.WriteLine("question is ", securityQuestion.Text)
【问题讨论】:
-
你试过
.getAttribute('textContent')吗? -
你也可以试试
.getAttribute("innerHTML") -
我尝试了这两种方法,但它们似乎不起作用。我唯一的选择可能只是一种使用某种屏幕阅读的方法,比如我真的不想做的 OCR。
标签: c# selenium xpath css-selectors webdriverwait