簡體   English   中英

硒webdriver facebook注銷C#

[英]selenium webdriver facebook logout C#

我是Selenium Webdriver的新手,我嘗試使用下面的代碼從Facebook注銷,但無法注銷。 我收到的異常是NoSuchElementException,因為代碼無法在下拉箭頭上發送click命令。

我嘗試了等待時間以及通過By.ID進行使用,但仍然遇到相同的問題。

知道我該如何解決嗎?

謝謝Facebook的注銷按鈕屏幕截圖

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;

namespace TestFrameWork.TC
{
    class LogIn
    {
        [Test]
        public void Test()
        {
            IWebDriver driver = new ChromeDriver();
            driver.Url = "https://www.facebook.com/";

            //Find the element that's ID attribute is 'account'(My Account)
            //driver.FindElement(By.XPath(".//*[@id='account']/a")).Click();

            // Find the element that's ID attribute is 'log' (Username)
            // Enter Username on the element found by above desc.
            driver.FindElement(By.Id("email")).SendKeys("your email");

            // Find the element that's ID attribute is 'pwd' (Password)
            // Enter Password on the element found by the above desc.
            driver.FindElement(By.Id("pass")).SendKeys("Your Pass");

            //Submit the Form
            driver.FindElement(By.Id("u_0_2")).Click();

            //Finding the Logout button
            //driver.FindElement(By.XPath(".//*[@id='userNavigationLabel']")).Click();

            //driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30));

            //Click on Logout Button
            driver.FindElement(By.Id("u_17_3")).Click();

            // Find the element that's ID attribute is 'account_logout' (Log Out)
            //driver.FindElement(By.XPath(".//*[@id='account_logout']/a")).Click();

            //Close Driver
            driver.Quit();
        }
    }
}

可能是定位器的問題,因為在檢查注銷按鈕時,我沒有找到與您的注銷定位器匹配的對象。

您可以設置chrome選項來禁用位置通知,如下所示:

Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_setting_values.notifications", 2);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
System.setProperty("webdriver.chrome.driver","C:/GitProject/ChromeDriver/chromedriver.exe");
WebDriver driver = new ChromeDriver(options);

我可以單擊下面的代碼單擊注銷,希望對您有所幫助。

//to click on the logout menu
driver.FindElement(By.XPath("//div[@id='logoutMenu']")).Click();

//to click on the logout 
driver.FindElement(By.XPath("//div[@class='uiScrollableAreaContent']//li//*[text()='Log Out']")).Click();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM