我已经尝试解决这个问题整整一周了,这是我最后一次尝试(询问堆栈溢出(。
我使用带有硒的phantomjs转到YouTube的登录页面并填写凭据并登录。
我进入登录页面,它设法填写了电子邮件,但无论我尝试什么,它都不会点击"下一步"按钮。
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')
现在我已经尝试了所有这些
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命令。
我还想补充一点,单击该元素对于没有 PhantomJS 的硒效果很好。
如果这里有人可以帮助我,我将不胜感激。
编辑:
此信息可能会有所帮助。单击"下一步"后,大约需要一秒钟才能进入密码部分。这是一个滑动动画。
这个问题尚未得到解答。
以下是您问题的答案:
几句话:
- 用于标识
Sign in
按钮的定位器不是唯一的。请考虑为Sign in
按钮构造唯一的xpath
。 - 用于标识
Email or phone
的定位器也需要稍作修改。 - 您可以考虑使用定位器
id
来识别并单击Next
按钮。 -
这是执行相同操作并在控制台上打印出
Clicked on Next Button
的代码块。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")
让我知道这是否回答了您的查询。