PhantomJS没有点击按钮

时间:2017-05-25 19:11:25

标签: c# selenium phantomjs

我正在尝试登录Youtube以通过我的视频获利,但它不会登录。我已经检查了元素是否存在,当它到达代码时单击它确实存在的按钮但似乎没有点击它:

我的代码:

IWebDriver driver = new PhantomJSDriver();
driver.Navigate().GoToUrl("https://www.youtube.com/my_videos?o=U");

// Fill in the email field
Thread.Sleep(1000);
driver.FindElement(By.Id("identifierId")).SendKeys(Email);
Thread.Sleep(1000);
driver.FindElement(By.Id("identifierNext")).Click();
Thread.Sleep(1000);

// Fill out the password field
driver.FindElement(By.Name("password")).SendKeys(Password);
driver.FindElement(By.XPath("//*[@id=\"passwordNext\"]")).Click();
Thread.Sleep(10000);

//Select the channel if we have more than 1
driver.FindElement(By.XPath("//span[.='" + Name + "']")).Click();

// Select all videos
driver.FindElement(By.XPath("//*[@id=\"non-appbar-vm-video-actions-bar\"]/div/span[2]/input")).Click();
Thread.Sleep(1000);

// Click the More button                               
driver.FindElement(By.XPath("//*[@id=\"non-appbar-vm-video-actions-bar\"]/div/div[1]/button")).Click();
Thread.Sleep(1000);

// Click Monotime link
driver.FindElement(By.XPath("//*[starts-with(@id, 'aria-menu-id')]/descendant::button/span[text()='Monetise']")).Click();
Thread.Sleep(1000);

// Monetize the video
driver.FindElement(By.XPath("//*[@id=\"body-container\"]/div[5]/div/div/div[1]/div[2]/span/div/div[2]/div[2]/button[2]")).Click();
Thread.Sleep(20000);

//Close browser
driver.Dispose();
driver.Quit();
driver.Close();

我收到错误:

enter image description here

如果我使用FirefoxDriver,上面的代码可以工作,但是使用PhantomJS无效。

如何点击Google的“下一步”按钮?

按钮的html:

<div role="button" id="identifierNext" class="O0WRkf zZhnYe e3Duub C0oVfc Zp5qWd Hj2jlf qs41qe M9Bg4d" jscontroller="VXdfxd" jsaction="click:cOuCgd; mousedown:UX7yZ; mouseup:lbsD7e; mouseenter:tfO1Yc; mouseleave:JywGue;touchstart:p6p2H; touchmove:FwuNnf; touchend:yfqBxc(preventMouseEvents=true|preventDefault=true); touchcancel:JMtRjd;focus:AHmuwe; blur:O22p3e; contextmenu:mg9Pef;" jsshadow="" jsname="tJiF1e" aria-disabled="false" tabindex="0">
    <div class="Vwe4Vb MbhUzd" jsname="ksKsZd" style="top: 18px; left: 44px; width: 88px; height: 88px;"></div>
    <div class="ZFr60d CeoRYc"></div>
    <content class="CwaK9"><span class="RveJvd snByac">Next</span></content>
</div>

我也尝试过,没有运气:

            TimeSpan ts = TimeSpan.FromSeconds(5);
            IWebDriver driver = new PhantomJSDriver();
            WebDriverWait wait = new WebDriverWait(driver, ts);

            driver.Navigate().GoToUrl("https://www.youtube.com/my_videos?o=U");

            // Fill in the email field
            IWebElement web = wait.Until(ExpectedConditions.ElementExists(By.Id("identifierId")));
            web.SendKeys(Email);

            IWebElement nxt = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//*[@id=\"identifierNext\"]/content/span")));
            nxt.Click();

            driver.TakeScreenshot().SaveAsFile("screenshot.png", ImageFormat.Png);

            // Fill out the password field
            wait.Until(ExpectedConditions.ElementToBeClickable(By.Name("password"))).SendKeys(Password);
            wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//*[@id=\"passwordNext\"]"))).Click();
            driver.TakeScreenshot().SaveAsFile("screenshot2.png", ImageFormat.Png);

            //Select the channel if we have more than 1
            wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//span[.='" + Name + "']"))).Click();

            // Select all videos
            wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//*[@id=\"non-appbar-vm-video-actions-bar\"]/div/span[2]/input"))).Click();

            // Click the More button                               
            wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//*[@id=\"non-appbar-vm-video-actions-bar\"]/div/div[1]/button"))).Click();

            // Click Monotime link
            wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//*[starts-with(@id, 'aria-menu-id')]/descendant::button/span[text()='Monetise']"))).Click();

            // Monetize the video
            wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//*[@id=\"body-container\"]/div[5]/div/div/div[1]/div[2]/span/div/div[2]/div[2]/button[2]"))).Click();

            //Close browser
            driver.Dispose();
            driver.Quit();
            driver.Close();

2 个答案:

答案 0 :(得分:0)

根据您提供的HTML,我会尝试点击<span>代替:

driver.FindElement(By.XPath("//*[@id=\"identifierNext\"]/content/span")).Click();

除此之外:您可能会看到new Google sign-in page的效果。

答案 1 :(得分:0)

就我而言,我发现如果您更改其他内容(例如Chrome浏览器的用户代理)的用户代理值,您就可以点击Google登录体验的“下一步”按钮PhantomJS网络驱动程序。

phantomJsOptions.AddAdditionalCapability("phantomjs.page.settings.userAgent"
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like 
     Gecko) Chrome/60.0.3112.113 Safari/537.36");

似乎可以根据浏览器的用户代理以不同方式呈现JavaScript。

相关问题