Skip to main content
 首页 » 编程设计

c#之如何使用 Selenium webdriver 获取不在 html 标签内的元素文本

2024年02月05日33Free-Thinker

下面是 html,我想获取文本“Conference USA”

<li class="list-unstyled" style="background-color: transparent;"> 
    <input id="account" type="checkbox" checked="checked" value="101" name="account"  
    style="background-color: rgb(255, 255, 255);"/> Conference USA                                  
</li> 

请您参考如下方法:

根据您的问题和提供的评论,您并不清楚您到底想要什么。

因此,如果您希望 li 元素使用文本 Conference USA,请尝试以下操作:-

WebElement liElement = driver.FindElement(By.Xpath("//li[contains(.,'Conference USA')]")); 

或者如果你想要 input 元素使用文本 Conference USA 尝试如下:-

WebElement inputElement = driver.FindElement(By.Xpath("//li[contains(.,'Conference USA')]/input")); 

或者如果你想要 li 元素的文本 Conference USA 尝试如下:-

string text = driver.FindElement(By.Xpath("//li[./input[@type = 'checkbox' and @value='101']]")).Text; 

希望对您有所帮助...:)