2016-12-19 9 views
0

Я новичок в selenium webdriver, и сегодня у меня есть некоторые проблемы с моим примером входа и выхода из системы. Я хочу получить элемент в gmail, но eclipse всегда замечает ошибку. Element i want to get (my email test)Построить Xpath, когда получить элемент в java

И вот мой xpath: //*[@id='gb']//descendant::div[@class='gb_vb']. Но при поиске, eclipse всегда замечает ошибку.

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: //*[@id='gb']//descendant::div[@class='gb_vb'] 
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html 
Build info: version: '3.0.1', revision: '1969d75', time: '2016-10-18 09:49:13 -0700' 
System info: host: 'Alex', ip: '192.168.1.7', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_112' 
Driver info: org.openqa.selenium.firefox.FirefoxDriver 
Capabilities [{rotatable=false, raisesAccessibilityExceptions=false, marionette=true, firefoxOptions={args=[], prefs={}}, appBuildId=20160623154057, version=, platform=XP, proxy={}, command_id=1, specificationLevel=0, acceptSslCerts=false, browserVersion=47.0.1, platformVersion=10.0, XULappId={ec8030f7-c20a-464f-9b0e-13a3a9e97384}, browserName=Firefox, takesScreenshot=true, takesElementScreenshot=true, platformName=Windows_NT, device=desktop}] 
Session ID: 2fd78092-bf4a-4095-8a32-1a2e1d22ed3e 
*** Element info: {Using=xpath, value=//*[@id='gb']//descendant::div[@class='gb_vb']} 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) 
    at java.lang.reflect.Constructor.newInstance(Unknown Source) 
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:127) 
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:93) 
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:42) 
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:163) 
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82) 
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601) 
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:368) 
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:473) 
    at org.openqa.selenium.By$ByXPath.findElement(By.java:361) 
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:360) 
    at CucumberTest.LoginTestFunction.WaitandElement.findE(WaitandElement.java:19) 
    at CucumberTest.LoginTestFunction.SeleniumTest.main(SeleniumTest.java:30) 
+0

Добавить 'HTML' целевого элемента вместо его изображений – Andersson

+0

@Andersson извините, но им не знают, как добавить рис в StackOverflow. им новичок здесь. Я пытаюсь использовать , но просто покажу ссылку. –

+0

Не добавляйте изображение, добавляйте 'HTML' в виде текста (скопируйте его с консоли разработчика) – Andersson

ответ

0

В этом случае вы должны использовать Explicit Waits

Причину, возможно, веб-браузер, нужно время, чтобы загрузить все элементы (например: ленивый груз, более, Аякс .. .)

//wait for 10 seconds 
WebDriverWait logWait = new WebDriverWait(driver, 10); 
logWait.until(ExpectedConditions.presenceOfElementLocated(by)); 
//now you can find element 
driver.findElement(By.xpath("//*[@id='gb']//descendant::div[@class='gb_vb']")); 
+0

Благодарим вас за полезную информацию, попробуйте и удачи –

0

Вы открываете меню, в котором элемент содержится первым? Я только что попробовал это, и он будет работать, только если вы откроете содержащее меню, прежде чем пытаться найти элемент, содержащий ваш адрес электронной почты заранее, например. (C# для примера):

IWebElement popupMenu = driver.FindElement(By.XPath("//*[@id=\"gb\"]/div[1]/div[1]/div[2]/div[4]/div[1]/a/span")); 
popupMenu.Click(); 

Тогда, пожалуйста, используйте этот код, чтобы найти тот элемент, который вы ищите.

+0

вы правы, сначала нужно отобразить это меню, а затем получить его текст. Но я нашел свою проблему о явном ожидании. –

0

Попробуйте XPath:

//div[@class="gb_vb"][contains(text(), "@gmail.com")]