我试过这段代码。它自动化浏览器,成功登录但我无法成功注销。请帮助。
public class fb {
public static void main(String args[])
{
WebDriver driver = new FirefoxDriver();
driver.get("https://facebook.com");
driver.manage().window().maximize();
driver.findElement(By.xpath(".//*[@id='email']")).sendKeys("your email");
driver.findElement(By.xpath(".//*[@id='pass']")).sendKeys("your password");
driver.findElement(By.xpath(".//*[@id='u_0_n']")).click();
driver.findElement(By.xpath(".//*[@id='userNavigationLabel']")).click();
driver.findElement(By.id("u_7_2")).click();// for logout-> button.But not working.
答案 0 :(得分:0)
尝试使用以下代码单击注销按钮
//wait for the userNavigationLabel to load fully any wait techniques implicit or explicit for sample i have used Thread.sleep although it is not recommended
Thread.sleep(3000);
JavascriptExecutor js = (JavascriptExecutor) driver;
String script = "var forms = document.getElementsByTagName('form');for (var i = 0; i < forms.length; i++) { if(forms[i].getAttribute('action')=='https://www.facebook.com/logout.php'){forms[i].submit();}}";
js.executeScript(script);
在上面的代码中,我使用javascript点击了注销按钮...我在浏览器控制台上测试了它工作正常
希望这会对你有所帮助.....如果您有任何疑问,请回复
答案 1 :(得分:0)
FaceBook登录和注销的自动化脚本:
WebDriver driver = new FirefoxDriver();
driver.get("https://www.facebook.com/");
driver.manage().window().maximize();
driver.findElement(By.xpath(".//*[@id='email']")).sendKeys("your email");
driver.findElement(By.xpath(".//*[@id='pass']")).sendKeys("your password");
//click for login
driver.findElement(By.xpath("//label[@id='loginbutton']/input")).click();
//Mouse over on logOut drop down menu icon,then click logout
WebElement mouseOverEle = driver.findElement(By.id("pageLoginAnchor"));
Actions actions = new Actions(driver);
actions.moveToElement(mouseOverEle).click().perform();
//click for logout
driver.findElement(By.xpath("//input[@value='Log Out']")).click();
答案 2 :(得分:0)
try:
lsb=browser.find_element_by_xpath('//*[@id="pageLoginAnchor"]')
lsb.click()
time.sleep(5)
mlb=browser.find_element_by_xpath('//*[@id="u_d_3"]/div/div/div[1]/div/div/ul/li[16]')
time.sleep(2)
mlb.click()
inputElement33 = browser.find_element_by_name('pass').is_displayed()
print("Looged out")
except:
print("Cannot logout")
答案 3 :(得分:0)
// Enter you Email id
dr.findElement(By.xpath(".//*[@id='email']")).sendKeys("YorEmailID");
// Enter you Password
dr.findElement(By.xpath(".//*[@id='pass']")).sendKeys("YourPassword");
//click for login
dr.findElement(By.xpath("//label[@id='loginbutton']/input")).click();
dr.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
// Click for Alert
dr.findElement(By.className("_3ixn")).click();
//Click for Navigation Label
dr.findElement(By.xpath(".//*[@id='userNavigationLabel']")).click();
dr.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//Click for Lout Button
dr.findElement(By.xpath("//a[contains(@data-gt,'menu_logout')]")).click();
答案 4 :(得分:0)
用此替换您的注销按钮。
//Wait for button appear
WebDriverWait wait = new WebDriverWait(driver,TimeSpan.FromSeconds(4));
wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//a[.='Log out']")));
//Click it by xpath
driver.FindElement(By.XPath("//a[.='Log out']")).Click();
或者:
//Wait for button appear
WebDriverWait wait = new WebDriverWait(driver,TimeSpan.FromSeconds(4));
wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//a[.='Log out']")));
//Click it by JS
IJavaScriptExecutor js = ((IJavaScriptExecutor)driver);
IWebElement logout = driver.FindElement(By.XPath("//a[.='Log out']"));
js.ExecuteScript("arguments[0].click();", logout);
100%工作
答案 5 :(得分:0)
**#Working script**
**#firefox can be used too instead of chrome**
**#sleep is necessary to give sufficient loading time**
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.common.exceptions import NoSuchElementException
usr=input('Enter Email Id:')
pwd=input('Enter Password:')
driver = webdriver.Chrome()
driver.get('https://www.facebook.com/')
a = driver.find_element_by_id('email')
a.send_keys(usr)
b = driver.find_element_by_id('pass')
b.send_keys(pwd)
c = driver.find_element_by_id('loginbutton')
c.click()
sleep(20)
d= driver.find_element_by_id("userNavigationLabel")
d.click()
sleep(10)
e=driver.find_element_by_xpath("//a[contains(@data-gt,'menu_logout')]")
e.click()
sleep(5)
driver.quit()
答案 6 :(得分:0)
主页中有重复的对象。为了避免混淆,Xpath可以用类名组合编写,在我们的例子中是
//div[@class='uiScrollableAreaBody']/following::div[@class='uiScrollableAreaContent']/following::ul/li
获取<li>
中<ul>
个标签的最大数量,如下所示,
List<WebElement> li = driver.findElements(By.xpath("//div[@class='uiScrollableAreaBody']/following::div[@class='uiScrollableAreaContent']/following::ul/li"));
System.out.println("Size:"+li.size());
注销按钮将始终是最后一个选项,尽管列表有任何更改,因此将最后一个索引附加到xpath并单击它,
String logOutBtn = "//div[@class='uiScrollableAreaBody']/following::div[@class='uiScrollableAreaContent']/following::ul/li[" + li.size() + "]";
driver.findElement(By.xpath(logOutBtn)).click(); -- Clicking on log out button
希望这有帮助!谢谢..
答案 7 :(得分:-1)
package package1;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class class1 {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\\Users\\CloudLogic.Tech01\\Desktop\\Selenium\\chromedriver_win32\\chromedriver.exe");
WebDriver drr = new ChromeDriver();
drr.get("https://www.facebook.com/");
drr.findElement(By.id("email")).sendKeys("emailId");
drr.findElement(By.id("pass")).sendKeys("********");
drr.findElement(By.xpath("//label[@id='loginbutton']")).click();
}
}