简体   繁体   English

找不到网页元素

[英]cannot locate a web element

I just want to write a simple log in script for Apple website: Sign In 我只想为Apple网站编写一个简单的登录脚本: 登录

The ID and password form cannot be located properly. 无法正确找到ID和密码表格。 Actually, I tried a lot of thing like: 实际上,我尝试了很多类似的事情:

 driver.find_element_by_xpath("//*[@type='email']")

or 要么

 driver.find_element_by_xpath("//*[@name='login-appleId']")

and

 driver.find_element_by_xpath("//*[@id='login-appleId']")

I did not find any iframe in this page. 我在此页面中找不到任何iframe。 And I tried same thing for customer checkout button, also same problem happens. 我为客户结帐按钮尝试了同样的事情,同样的问题也发生了。

Any suggestions would be appreciate! 任何建议,将不胜感激!

Best, Luke 最好,卢克

You can Follow this code .. It works ..!! 您可以按照此代码进行操作。

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

driver = webdriver.Chrome('E:\chromedriver.exe') #location
driver.get('url') #https://secure2.store.apple.com/shop/sign_in?c=aHR0cDovL3d3dy5hcHBsZS5jb20vc2hvcC9iYWd8MWFvczVjNGU3ZWNjZjgwODVjNWY4NDk0OTA0ODJhMDc2Y2FkNmU3ODJkOTE&o=O01LV0gy&r=SXYD4UDAPXU7P7KXF&s=aHR0cHM6Ly9zZWN1cmUyLnN0b3JlLmFwcGxlLmNvbS9zaG9wL2NoZWNrb3V0L3N0YXJ0P3BsdG49RkNBRjZGQjR8MWFvczAyZmZkZjQwNTgwOGI4ZTNkMDQ5MWRiM2NmZmExYTgxNzRkZTllMjY&t=SXYD4UDAPXU7P7KXF&up=t

def find_by_xpath(locator):
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, locator))
    )
   return element
class FormPage(object):
    def fill_form(self, data):
        find_by_xpath('//input[@name = "login-appleId"]').send_keys(data['usr')
        find_by_xpath('//input[@name = "login-password"]').send_keys(data['pwd'])
        return self 

    def submit(self):
        find_by_xpath('//input[@id = "sign-in"]').click()

 data = {
       'usr': 'xx@apple.com',
       'pwd': 'xxxx'
       }

if __name__=="__main__":
     FormPage().fill_form(data).submit()
     driver.quit() # closes the webbrowser

Hope it is helpful to you Thanks.!! 希望对您有帮助谢谢!!

I recommend you try the following: 我建议您尝试以下操作:

driver.find_element_by_id("login-appleId")
driver.find_element_by_id("login-password")

Sometimes in WebDriver there are scenarios where the WebElement isn't loaded properly on DOM and webdriver tries to find it. 有时,在WebDriver ,有些情况下WebElement无法在DOM上正确加载,而Webdriver试图找到它。 So to handle these kind of scenarios there are 2 types of wait provided by WebDriver library. 因此,要处理这类情况, WebDriver库提供了两种类型的等待。

You just need to implement one of these based on your requirements. 您只需要根据自己的需求实施其中之一。

  1. Implicit Waits 隐式等待
  2. Explicit Waits 显式等待

I suggest you to implement one of these and then try to execute your script. 我建议您实现其中之一,然后尝试执行脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM