我有一個包含特定搜索查詢結果的div。包含在該div中的文本隨着點擊按鈕進入下一頁而改變。Selenium for Python - text_to_be_present用於部分文本匹配
在該div中包含的文本中,還有頁面的相應編號。點擊按鈕轉到下一頁後,結果仍然需要加載一點,所以我想讓驅動程序等待內容加載。
因此,我希望等到字符串「頁面x」出現在div內,其中x對應於要加載的下一頁的頁碼。
對於這一點,我想:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
# Search page
driver.get('http://searchpage.com')
timeout = 120
current_page = 1
while True:
try:
# Wait until the results have been loaded
WebDriverWait(driver, timeout).until(
EC.text_to_be_present_in_element(
locator=(By.ID, "SEARCHBASE"),
text_="Page {:}".format(current_page)))
# Click to go to the next page. This calls some javascript.
driver.find_element_by_xpath('//a[@href="#"]').click
current_page += 1
except:
driver.quit()
雖然,這總是不匹配文本。我在這裏做錯了什麼?
要檢測頁面中是否發生了任何更改也可以完成這項工作,但我還沒有找到任何方法可以做到這一點。
你的意思是'的DIV#SEARCHFORMBASE'全文內容爲「「一些文本1」''」一些文本第2頁「'...? – Andersson
@安德森是的。 'Page x'後面還有一些文字 –