Selenium Python get_element通过ID失败 [英] Selenium Python get_element by ID failing

查看:583
本文介绍了Selenium Python get_element通过ID失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我理解为什么我的代码无法通过ID查找元素.下面的代码:

Can someone help me understand why my code fails to find the element by ID. Code below:

from selenium import webdriver
driver=webdriver.Firefox()
driver.get('https://app.waitwhile.com/checkin/lltest3/user')
element = driver.find_element_by_id("guestPhone")

检查元素清楚地显示了ID.

Inspecting element shows the ID clearly.

<input type="tel" name="guestPhone" id="guestPhone" class="form-control ng-pristine ng-empty ng-invalid ng-invalid-phone-validator ng-invalid-required ng-touched" ng-model="form.model" ng-model-options="{ 'updateOn': 'default blur', 'debounce': { 'default': 350, 'blur': 0 } }" uib-typeahead="guest.phone for guest in form.onChange({value:$viewValue})" typeahead-min-length="6" typeahead-on-select="form.onSelect({guest:$item})" typeahead-select-on-exact="true" uib-tooltip="Please enter valid number. Include country code for non-US numbers" tooltip-trigger="'none'" tooltip-is-open="(form.guestForm.$submitted || form.guestForm.guestPhone.$touched) &amp;&amp; form.guestForm.guestPhone.$invalid" tooltip-placement="bottom" ng-required="::form.required" phone-validator="US" placeholder="Mobile phone" title="Please enter a valid phone number" autocomplete="nope" next-on-enter="" aria-autocomplete="list" aria-expanded="false" aria-owns="typeahead-47-2884" required="required" style="">

P.S.我也尝试过XPath和名称.还是没有运气.

P.S. I've also tried XPath and name as well. Still no luck.

推荐答案

您需要等待该元素在页面上可见.您可以说它是动态加载的,因为如果右键单击chrome页面并查看源代码,您将看到没有guestPhone元素.它已使用javascript加载

You need to wait for the element to become visible on the page. You can tell this is loaded in dynamically because if you right-click on the page in chrome and view source you'll see there's no guestPhone element. It gets loaded in with javascript

以下是 http://isaacviel.name/中的示例使网络驱动程序等待元素变得可见/:

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.Firefox()
driver.get("http://somedomain/url_that_delays_loading")
try:
    element = WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.ID, "myDynamicElement"))
    )
finally:
    driver.quit()

这篇关于Selenium Python get_element通过ID失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆