简体   繁体   English

Python Selenium查找元素

[英]Python Selenium finding element

I need your help for a strange problem. 遇到一个奇怪的问题,我需要您的帮助。 I am using Selenium library and I have problem with this instruction: 我正在使用Selenium库,此指令存在问题:

driver.find_element_by_name('longitude')

Sometimes it works and I can find the element with the name longitude and sometimes not even if nothing changes in the web page. 有时它可以工作,我可以找到带有经度名称的元素,有时即使网页中没有任何变化也找不到。

Use wait method before finding the element:- eg. 找到元素之前使用wait方法:-例如。

from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.support.ui import WebDriverWait

WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID,"element")))

使用time.sleep(seconds)加载页面,然后在您找到Web元素之后,有时如果页面未加载,它可能会像Web元素不可用一样抛出,因此请使用time.sleep()

You can implicit wait for that : 您可以隐式等待:

Something like : 就像是 :

from selenium import webdriver

driver = webdriver.Firefox()
driver.implicitly_wait(10) # seconds
driver.get("http://somedomain/url_that_delays_loading")
driver.find_element_by_name('longitude')

OR 要么

Explicit wait : 显式等待

WebDriverWait(driver, 10).until( EC.visibility_of_element_located((By.NAME,"longitude"))) 

For this you have to import these : 为此,您必须导入以下内容:

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

Please Note that you should not be mixing implicit wait with explicit wait , doing so can cause unpredictable wait time out. 请注意,您不应将隐式等待显式wait混合使用,否则可能导致无法预测的 等待超时。

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

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