簡體   English   中英

Python + Selenium:元素不可見

[英]Python + Selenium: element not visible

這是網頁鏈接 我要爬行(在波斯語中)。 當我要點擊下一頁按鈕時,我遇到了問題。 XPath是:

nextpage = '//*[@id="ctl00_ContentPlaceHolder1_ASPxSplitter1_CallbackPaneldgd_dgd_DXPagerBottom"]/a[1]/img'
page = driver.find_element_by_xpath(nextpage)
page.click()

page.click()之后,我收到以下錯誤:

selenium.common.exceptions.ElementNotVisibleException: Message: element not visible

一些答案說可能存在重復的XPath,但我在網頁的源代碼中找不到這樣的東西。

完整的代碼:

import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
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
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains


driver = webdriver.Chrome(executable_path='./chromedriver')
url = 'http://hmi.mrud.ir/sabaa/SABAA/Home/Default.aspx?strTownship=0101&g=false'
driver.get(url) 
time.sleep(10)   
nextpage = '//*[@id="ctl00_ContentPlaceHolder1_ASPxSplitter1_CallbackPaneldgd_dgd_DXPagerBottom"]/a[1]/img'
page = driver.find_element_by_xpath(nextpage)
page.click()

謝謝。

在單擊元素之前使用以下內容:

driver.execute_script("arguments[0].scrollIntoView(true);", page)

這是滾動到元素。

希望它能幫到你!

根據Website如果要單擊“ Next Page Button ,則必須引導WebDriverWait ,以便可以按照以下任一代碼塊單擊WebElement

nextpage = WebDriverWait(driver, 10).until(EC.element_to_be_clickable(By.XPATH,"//div[@id='ctl00_ContentPlaceHolder1_ASPxSplitter1_CallbackPaneldgd_dgd_DXPagerBottom']/a[@class='dxp-lead dxp-button dxp-bi']/img[@class='dxWeb_pPrev_Aqua']"))
nextpage.click()

要么

nextpage = WebDriverWait(driver, 10).until(lambda x: x.find_element_by_xpath("//div[@id='ctl00_ContentPlaceHolder1_ASPxSplitter1_CallbackPaneldgd_dgd_DXPagerBottom']/a[@class='dxp-lead dxp-button dxp-bi']/img[@class='dxWeb_pPrev_Aqua']"))
nextpage.click()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM