首页 > 解决方案 > 为什么Webdriver无法通过xpath找到元素?

问题描述

我的代码找不到带有XPath. 使用同样的问题CSSSelector

driver = new ChromeDriver(options);
driver.Navigate().GoToUrl("https://www.goldtoe.com/state/landing");         
var LoginLink = driver.FindElement(By.XPath("//li[@class='hidden-xs myAccountMenu']//a[contains(text(),'Sign In/Register')] "));
LoginLink.Click();

信息:

> OpenQA.Selenium.NoSuchElementException : no such element: Unable to
> locate element: {"method":"xpath","selector":"//li[@class='hidden-xs
> myAccountMenu']//a[contains(text(),'Sign In/Register')] "}   (Session
> info: chrome=74.0.3729.169)   (Driver info: chromedriver=74.0.3729.6
> (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Windows
> NT 10.0.17763 x86_64)

标签: .netselenium-webdriverxpathchrome-web-driver

解决方案


我认为“登录/注册”字符串需要一个转义字符,特别是“/”特殊字符。因此,请尝试“登录\/注册”。使用 '\' 作为转义字符。


推荐阅读