硒无法定位元素

时间:2017-11-22 16:06:36

标签: python css selenium

硒无法定位元素。 错误信息:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"loadAllUpcomingPast"}

我的代码是:

url_base = 'http://www.christies.com/lotfinder/searchresults.aspx&searchtype=p&action=paging&searchFrom=header&lid=1&entry=&pg=all'

driver = webdriver.Chrome()

driver.get(url_base)

time.sleep(2)

driver.switch_to.frame("signupFrame")

driver.find_element_by_id("close_signup").click()

time.sleep(2)
driver.find_element_by_id("loadAllUpcomingPast").click()

截图

enter image description here

1 个答案:

答案 0 :(得分:0)

似乎稍后会加载loadAllUpcomingPast。你可以尝试这样的事情:

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


url_base = 'http://www.christies.com/lotfinder/searchresults.aspx?&searchtype=p&action=paging&searchFrom=header&lid=1&entry=lama&pg=all'
driver = webdriver.Chrome()
driver.get(url_base)

wait = WebDriverWait(driver, 5)

try:
    element = wait.until(EC.element_to_be_clickable((By.ID, 'loadAllUpcomingPast')))
    print(f'Element found: {element}')
except TimeoutException:
    print('could not find loadAllUpcomingPast')

在我的控制台中,我看到:

Element found: <selenium.webdriver.remote.webelement.WebElement (session="c9f1b83164f34001aa0a98581618d45b", element="0.6182317130585246-1")>

这将告诉Selenium等待该元素可点击。如果需要,您可以在WebDriverWait第二个参数中调整等待时间。