-->

Python | PhantomJS not clicking on element

2020-08-01 05:28发布

问题:

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

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()

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

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.

EDIT:

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.

回答1:

Here is the Answer to your Question:

A couple of words:

  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.