gpt4 book ai didi

java - 如何使用 Java 解决使用 Selenium 退出 Facebook 的问题

转载 作者:行者123 更新时间:2023-12-02 03:39:13 25 4
gpt4 key购买 nike

我已经搜索了很多关于如何使用 Selenium 注销 Facebook 的信息。我仔细研究了 StackOverflow 和许多其他使用 Selenium 涵盖该主题的网站上的问题,尽管没有成功。我正在学习 Selenium,并认为它非常简单,直到遇到这个障碍。无论我做什么,Selenium 都不会注销 Facebook。下面是我正在使用的代码。我很感激任何指示(我使用 Firepath 来获取 xpath 地址):

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

public class GetFacebookTitle {

public String bu="https://www.facebook.com";
public WebDriver driver;



@BeforeSuite
public void betest(){
driver=new FirefoxDriver();
driver.get(bu);
}

@Test(priority=2)
public void test2()
{
String tilte=driver.getTitle();
System.out.println(tilte);
}

@Test(priority=1)
public void test1()
{
driver.get(bu);
driver.findElement(By.id("email")).sendKeys("name@hotmail.com");
driver.findElement(By.id("pass")).sendKeys("mypassword");
driver.findElement(By.id("loginbutton")).submit();
//System.out.println(title);
}

@Test(priority=3)
public void test3()
{
WebElement lstitem=driver.findElement(By.id("userNavigationLabel"));
lstitem.click();
lstitem.findElement(By.xpath("html/body/div[17]/div/div/div/div/div[1]/div/div/ul/li[16]/a/span/span")).click(); //This line just does not work xpath


}

}

这是返回的错误:

Facebook
PASSED: test1
PASSED: test2
FAILED: test3
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"html/body/div[17]/div/div/div/div/div[1]/div/div/ul/li[16]/a/span/span"}
Command duration or timeout: 33 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.52.0', revision: '4c2593c', time: '2016-02-11 19:03:33'
System info: host: 'RLaptop', ip: '192.168.xx.x', os.name: 'Windows 10', os.arch: 'x86', os.version: '10.0', java.version: '1.8.0_73'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=45.0.1, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: c5781a63-5d20-4a55-9cd6-7cd70aa38165
*** Element info: {Using=xpath, value=html/body/div[17]/div/div/div/div/div[1]/div/div/ul/li[16]/a/span/span}
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.ErrorHandler.createThrowable(ErrorHandler.java:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:327)
at org.openqa.selenium.remote.RemoteWebElement.findElement(RemoteWebElement.java:198)
at org.openqa.selenium.remote.RemoteWebElement.findElementByXPath(RemoteWebElement.java:295)
at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
at org.openqa.selenium.remote.RemoteWebElement.findElement(RemoteWebElement.java:194)
at Testing.GetFacebookTitle.test3(GetFacebookTitle.java:47)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:639)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:774)
at org.testng.TestRunner.run(TestRunner.java:624)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:359)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)
at org.testng.SuiteRunner.run(SuiteRunner.java:261)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1215)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
at org.testng.TestNG.run(TestNG.java:1048)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:137)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:58)
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"html/body/div[17]/div/div/div/div/div[1]/div/div/u l/li[16]/a/span/span"}
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.52.0', revision: '4c2593c', time: '2016-02-11 19:03:33'
System info: host: 'RLaptop', ip: '192.168.xx.x', os.name: 'Windows 10', os.arch: 'x86', os.version: '10.0', java.version: '1.8.0_73'
Driver info: driver.version: unknown
at <anonymous class>.FirefoxDriver.prototype.findElementInternal_(file:///C:/Users/Rozelle/App Data/Local/Temp/anonymous1905752364377844577webdriver- profile/extensions/fxdriver@googlecode.com/components/driver-component.js:10723)
at <anonymous class>.FirefoxDriver.prototype.findChildElement(file:///C:/Users/Rozelle/AppData /Local/Temp/anonymous1905752364377844577webdriver- profile/extensions/fxdriver@googlecode.com/components/driver-component.js:10735)
at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/Rozelle/AppD ata/Local/Temp/anonymous1905752364377844577webdriver- profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12614)
at <anonymous class>.DelayedCommand.prototype.executeInternal_(file:///C:/Users/Rozelle/AppDat a/Local/Temp/anonymous1905752364377844577webdriver- profile/extensions/fxdriver@googlecode.com/components/command- processor.js:12619)
at <anonymous class>.DelayedCommand.prototype.execute/<(file:///C:/Users/Rozelle/AppData/Local/Temp/anonymous1905752364377844577webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12561)

最佳答案

代替这一行

lstitem.findElement(By.xpath("html/body/div[17]/div/div/div/div/div[1]/div/div/ul/li[16]/a/span/span")).click();

使用下面的行,它应该可以工作。

driver.findElement(By.partialLinkText("Log Out")).click();

关于java - 如何使用 Java 解决使用 Selenium 退出 Facebook 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37041480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com