Python | PhantomJS没有点击元素 [英] Python | PhantomJS not clicking on element

查看:134
本文介绍了Python | PhantomJS没有点击元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经整整一个星期试图解决这个问题,这是我对此的最后一枪(询问stackoverflow).

I have been trying to solve this for an entire week now and this is my last shot at this (asking stackoverflow).

我将phantomjs和硒一起使用,以转到YouTube的登录页面并填写凭据并登录.

I use phantomjs with selenium to go to the login page of YouTube and fill in the credentials and log in.

我进入登录页面,它设法填写了电子邮件,但是无论我如何尝试,它都不会单击下一步"按钮.

I get to the login page and it manages to fill in the email, but no matter what I try, it won't click on the "next" button.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.action_chains import ActionChains
import time
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.ui import WebDriverWait

dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["-phantomjs.page.settings.userAgent-"] = (
    "-Mozilla-5.0 (Windows NT 6.3; WOW64) AppleWebKit-537.36 (KHTML, like Gecko) Chrome-34.0.1847.137 Safari-537.36-"
    )

driver = webdriver.PhantomJS(desired_capabilities=dcap)

driver.set_window_size(1920,1080)
driver.get("https://youtube.com")
driver.find_element_by_class_name("yt-uix-button-content").click()
print("Logging in...")
driver.find_element_by_id("identifierId").send_keys("email")
time.sleep(1)
driver.find_element_by_class_name("ZFr60d").click()

driver.save_screenshot('testing4.png')

现在我已经尝试了所有这些

Now I have tried all these

driver.find_element_by_xpath("""//*[@id="identifierNext"]/content/span""").click()

driver.find_element_by_css_selector("#identifierNext>content>span").click()

webdriver.ActionChains(driver).move_to_element(element).click(element).perform()

driver.find_element_by_id("identifierNext").click()

,但这些都不起作用.我也尝试了javascript命令.

and none of these works. I tried the javascript command aswell.

我还要补充一点,在没有PhantomJS的情况下,单击元素对于使用硒来说效果很好.

I would also like to add that clicking on the element works perfectly fine with selenium without PhantomJS.

如果有人能帮助我,我将不胜感激.

I would really appreciate it if anyone here could help me.

此信息可能会有所帮助.单击下一步"后,大约需要一秒钟才能进入密码部分.这是一个滑动动画.

This info might be helpful. After clicking "Next", it takes about a second to get to the password part. It's a sliding animation.

这个问题尚未得到答案.

This question has yet not been answered.

推荐答案

以下是您的问题的答案:

Here is the Answer to your Question:

几句话:

  1. 您用来标识Sign in按钮的定位器不是唯一的.考虑为Sign in按钮构造一个唯一的xpath.
  2. 您用来标识Email or phone的定位器也需要进行一些修改.
  3. 您可以考虑使用定位器id来识别并单击Next按钮.
  4. 这是相同的代码块,并在控制台上打印出Clicked on Next Button.

  1. The locator you have used to identify the Sign in button is not unique. Consider constructing an unique xpath for the Sign in button.
  2. The locator you have used to identify the Email or phone also needs to be modified a bit.
  3. You can consider to use the locator id to identify and click on the Next button.
  4. Here is the code block which does the same and prints out Clicked on Next Button on the console.

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["-phantomjs.page.settings.userAgent-"] = (
    "-Mozilla-5.0 (Windows NT 6.3; WOW64) AppleWebKit-537.36 (KHTML, like Gecko) Chrome-34.0.1847.137 Safari-537.36-"
    )

driver = webdriver.PhantomJS(desired_capabilities=dcap, executable_path="C:\\Utility\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe")

driver.get("https://youtube.com")
driver.find_element_by_xpath("//button[@class='yt-uix-button yt-uix-button-size-default yt-uix-button-primary']/span[@class='yt-uix-button-content']").click()
print("Logging in...")
email_phone = driver.find_element_by_xpath("//input[@id='identifierId']")
email_phone.send_keys("debanjanb")
driver.find_element_by_id("identifierNext").click()
print("Clicked on Next Button")

让我知道这是否回答了您的查询.

Let me know if this Answers your Query.

这篇关于Python | PhantomJS没有点击元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆