簡體   English   中英

無法點擊按鈕 Selenium

[英]Can't click on button Selenium

我在單擊此頁面上的“下一步”按鈕時遇到問題:

https://www.govinfo.gov/app/collection/uscourts/district/alsd/2021/%7B%22pageSize%22%3A%22500%22%2C%22offset%22%3A%220%22%7D

我試過 xpath,css_selector 和沒有骰子。 這是代碼:

try:
        next_page = driver.find_element_by_xpath('//*[@id="collapseOne3578"]/div/span[1]/div/ul/li[4]')
        if next_page:   
                print('Trying to get next page')
                print(next_page)
                next_page.click()
        else:
            continue
except:
    break

我將變量放在 try 語句中,因為某些頁面沒有超過 500 的結果,因此沒有下一步按鈕。 如果我在語句之外定義變量,則代碼會中斷。

謝謝你的幫助

您使用了錯誤的定位器。
請試試這個:

next_page = driver.find_element_by_xpath('//li[@class="next fw-pagination-btn"]')

或這個

next_page = driver.find_element_by_css_selector('li.next.fw-pagination-btn')

在定位元素之前不要忘記添加等待,最好是期待條件。

嘗試這個:

driver.find_element_by_xpath(".//ul[@class='pagination']/li/a[text()='Next']").click()

該元素不可見,因此我使用了:

next_page = WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH,'//li[@class="next fw-pagination-btn"]')))
driver.execute_script('arguments[0].click()', next_page)

它奏效了。

暫無
暫無

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

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