[英]How can I find a special element on a page?
我正在开始使用硒。 我打开了网址,并输入了用户名和密码: Tribal Wars 2 。 问题是到达此按钮以单击:
<a href="#" class="btn-orange btn-border small-icon" ng-click="selectCharacter(w.charId, w.id, w.loginDisabled)" tooltip="" tooltip-if="w.loginDisabled" tooltip-content="Diese Welt wurde geschlossen." ng-class="{'btn-grey': w.loginDisabled}" <span class="icon" <span class="icon-inner character" ng-class="{ 'character': playerId === w.ownerId, 'coop': playerId !== w.ownerId, 'locked': w.loginDisabled }"</span </span <span class="wrapper align-left" <span class="text character ng-binding"Rammboss</span <span class="text world ng-binding"Leeds Castle</span </span </a
Java代码:
driver.findElement(By.xpath("//*[@id=\"wrapper\"]/div[2]/div[3]/div/div[ 1]/div[3]/div[ 1]/ul/li/a")).click();
在端口20436上启动ChromeDriver(v2.9.248315),线程“主” org.openqa.selenium.NoSuchElementException中的异常:没有这样的元素
(会话信息:chrome = 53.0.2785.143)(驱动程序信息:chromedriver = 2.9.248315,platform = Windows NT 6.1 SP1 x86_64)(警告:服务器未提供任何堆栈跟踪信息)命令持续时间或超时:39毫秒有关文档发生此错误,请访问:seleniumhq.org/exceptions/no_such_element.html构建信息:版本:'2.43.1',修订版本:'5163bceef1bc36d43f3dc0b83c88998168a363a0',时间:'2014-09-10 09:43:55'系统信息:主机:'AGBioSA04',ip:'10 .4.1.81',操作系统名称:'Windows 7',os.arch:'amd64',os.version:'6.1',java.version:'1.8.0_102'驱动程序信息: org.openqa.selenium.chrome.ChromeDriver功能[{applicationCacheEnabled = false,rotatable = false,chrome = {userDataDir = C:\\ Users \\ Admin \\ AppData \\ Local \\ Temp \\ scoped_dir4760_1168},需要HeapSnapshot = true,databaseEnabled = false,handlesAlerts = true,版本= 53.0.2785.143,平台= XP,browserConnectionEnabled = false,nativeEvents = true,acceptSslCerts = true,locationContextEnabled = true,webStorageEnabled = true,browserName = chrome ,takesScreenshot = true,javascriptEnabled = true,cssSelectorsEnabled = true}]会话ID:位于sun.reflect.NativeConstructorAccessorImpl.newInstance0(本地方法)处的5f87f399ba0889cb549ffc090b9dffbf位于sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessor.java。 org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)上的java.lang.reflect.Constructor.newInstance(Constructor.java:423)上的DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)上的.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)在org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.findElement(RemoteWebDriver.java:599) :352),位于org.openqa.selenium.remote的org.openqa.selenium.By $ ByXPath.findElement(By.java:357)的org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:449)处。 RemoteWebDriver.findElemen t(RemoteWebDriver.java:344)在com.mysite.selenium.HomePage.fillField(HomePage.java:46)在com.mysite.selenium.HomePage.main(HomePage.java:58)在sun.reflect.NativeMethodAccessorImpl.invoke0 (本地方法)位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)位于sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)位于java.lang.reflect.Method.invoke(Method.java:498) ),位于com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
该游戏是用PHP和JavaScript编写的。 硒有可能做到这一点吗?
在chrome控制台上尝试了xpath,这是一个问题。
您可以简单地使用此xpath单击“更多信息” /“ Mehr信息”按钮
//div[@class='more-info']//a
如果您尝试单击登录按钮,请使用以下xpath之一
//button[span[.='Login']]
要么
//button[@class='button-login']
另外,尽管问题中的xpath不正确并且不返回任何匹配的节点,但为了将来参考,请不要在xpath中指定双引号,即\\“ wrapper \\”。 改为使用这种方式:
//*[@id='wrapper']
登录后,您可能需要等待元素。
WebDriverWait wait = new WebDriverWait(driver, 10); // Wait for 10 seconds.
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"wrapper\"]/div[2]/div[3]/div/div[ 1]/div[3]/div[ 1]/ul/li/a")));
WebElement element = driver.findElement(By.xpath("//*[@id=\"wrapper\"]/div[2]/div[3]/div/div[ 1]/div[3]/div[ 1]/ul/li/a"));
element.click();
希望能有所帮助。
编辑:以防万一您在查找xpath时遇到麻烦-在Chrome中:
是的,您可以使用硒,而不管被测语言是哪种开发的。 Selenium在黑盒级别上工作,因此它与Web应用程序的代码库没有任何关系。
关于您的问题,从异常来看,要么元素定位器错误,要么您需要等待元素出现。 您可以在firfox中使用firbug和firepath来确保您的定位器有效并指向正确的元素。
这些是提供自动化实施时需要注意的常见事项。 需要了解selenium或webdriver是api,并提供了一种与受测Web /移动应用程序进行交互的方式。 为了克服所有其他测试需求和常规自动化挑战,您应该创建一个照顾或开始使用可用的现有框架(例如QMetry Automation Framework)的基础框架。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.