硒-基于余烬的查找元素

问题描述:

我正在Chrome浏览器中的python 3.6中使用硒.我已经对其进行了编程,可以访问想要的网站,但是我一直在努力寻找要搜索的文本框元素.当我检查元素时,它具有以下代码.

I am working with selenium in python 3.6 on the chrome browser. I have programmed it to the point where I can access the website I want but I am struggling to find the text box element I am searching for. When I inspect the element it has this code.

<input placeholder="" id="ember32" class="ssRegistrationField ssEmailTextboxField ember-text-field ember-view" type="email">

但是当我尝试使用给定的ID时,它不起作用,并说找不到它.这是我的代码(没有我要在网站URL中插入的文字):

But when I try and use the given ID, it does not work and says that it cannot be found. Here is my code (Without the text I wish to insert of the website URL):

from selenium import webdriver

browser = webdriver.Chrome('chromedriver.exe')

browser.get('')

email = browser.find_element_by_id("ember34")
email.send_keys('')

我今天才刚开始使用Selenium,对找出问题所在的任何帮助将不胜感激.

I have just started using Selenium today and any help figuring out what is wrong would be very appreciated.

所需的元素是 Ember.js 元素,以便对元素进行 click(),您必须为 element_to_be_clickable()引入 WebDriverWait ,并且您可以使用以下任意一种定位器策略:

The desired element is an Ember.js element so to click() on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • 使用 CSS_SELECTOR :

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.ssRegistrationField.ssEmailTextboxField.ember-text-field.ember-view[id^='ember'][type='email']"))).send_keys("Max")

  • 使用 XPATH :

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='ssRegistrationField ssEmailTextboxField ember-text-field ember-view' and starts-with(@id,'ember')][@type='email']"))).send_keys("Max")
    

  • 注意:您必须添加以下导入:

  • Note : You have to add the following imports :

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

  • 您可以在以下位置找到几个相关的详细讨论:

    You can find a couple of relevant detailed discussions in: