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

问题描述

我一直在尝试为整个星期解决这个问题,这是我的最后一次镜头(询问stackoverflow).

我使用selenium的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命令.

我还想补充一下,单击元素与没有幻影的硒完全正常.

如果这里有人可以帮助我,我真的很欣赏它.

编辑:

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

这个问题尚未得到解答.

推荐答案

以下是您问题的答案:

几字:

  1. 您用于识别Sign in按钮的定位器不是唯一的.考虑为Sign in按钮构建唯一的xpath.
  2. 您用于识别Email or phone的定位器也需要修改一下.
  3. 您可以考虑使用locator id来识别并单击Next按钮.
  4. 这是一个代码块,它在控制台上的相同并打印出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")
    

让我知道这是否答案了你的查询.

本文地址:https://www.itbaoku.cn/post/1739984.html