HTML 示例:
<div class="lend-count">
<span>50</span>
</div>
<div class="lend-count">
<span>55</span>
</div>
<div class="lend-count">
<span>25</span>
</div>
<div class="lend-count">
<span>45</span>
</div>
<div class="lend-count">
<span>25</span>
</div>
<div class="lend-count">
<span>30</span>
</div>
<div class="lend-count">
<span>25</span>
</div>
<div class="lend-count">
<span>15</span>
</div>
<div class="lend-count">
<span>10</span>
</div>
在硒鉻網路驅動程式中。我試圖找到介于數字范圍 (10-20) 之間的第一個跨度,在本例中為15,然后單擊它。如果此頁面上沒有任何內容(拋出 NoSuchElementException),請單擊下一頁按鈕并回圈回傳重試:
while True:
try:
driver.find_element(By.XPATH, "//span[number(.)= <10, >20]").click()
time.sleep (1)
break
except NoSuchElementException:
driver.find_element(By.XPATH, "//*[@class='anticon anticon-right']").click()
time.sleep (1)
也嘗試使用范圍:
price = range (10,20)
driver.find_element(By.XPATH, "//span[number(.)= 'price']").click()
請指教
uj5u.com熱心網友回復:
driver.find_element(By.XPATH,"//div[@class='lend-count']/span[number()>10 and number()<20]").click()
只需使用 number()>10 和 number()<20 即可獲取范圍之間的數字。要確保它出現,請使用 webdriver 等待。
wait=WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH,"//div[@class='lend-count']/span[number()>10 and number()<20]"))).click()
如果出現具有這些值的跨度,將檢查 10 秒,然后拋出例外。
while True:
try:
wait.until(EC.element_to_be_clickable((By.XPATH,"//div[@class='lend-count']/span[number()>10 and number()<20]"))).click()
break
except NoSuchElementException:
wait.until(EC.element_to_be_clickable((By.XPATH,"//*[@class='anticon anticon-right']"))).click()
進口
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
uj5u.com熱心網友回復:
您可以使用 XPath 將所有 Web 元素存盤//span[text()]在如下串列中,然后使用回圈進行迭代并放置條件if int(span_text.text) < 10 and int(span_text.text) >20。
代碼:
while True:
try:
list_of_span_tag_text = driver.find_elements(By.XPATH, "//span[text()]")
for span_text in list_of_span_tag_text:
if int(span_text.text) < 10 and int(span_text.text) >20:
span_text.click()
time.sleep(1)
break
else:
print("None of the span on this page satisfy this condtion, span_text.text <10 and span_text.text > 20")
except NoSuchElementException:
driver.find_element(By.XPATH, "//*[@class='anticon anticon-right']").click()
time.sleep(1)
更新 1:
while True:
try:
list_of_span_tag_text = driver.find_elements(By.XPATH, "//span[text() < 20][text() > 10]")
for span_text in list_of_span_tag_text:
span_text.click()
time.sleep(1)
break
else:
print("None of the span on this page satisfy this condtion, span_text.text < and span_text.text > 20")
except NoSuchElementException:
driver.find_element(By.XPATH, "//*[@class='anticon anticon-right']").click()
time.sleep(1)
uj5u.com熱心網友回復:
您可以嘗試:while 1:
len_click = 0 #remains 0 if no spans are in range 10-20
try:
#contains all the span with number in range.
lst = driver.find_element(By.XPATH, "//span[number()>10][number()<20]")
len_click = len(lst)
if(click!=0): #shows no span exist in range 10 to 20.
lst[0].click
else:
#code for next page.
except NoSuchElementException:
#code for next page.
如果您遇到任何問題,請在下面發表評論。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/380020.html
上一篇:TypeError:WebDriver.__init__()在SeleniumPython中使用firefox_options作為引數得到了意外的關鍵字引數“firefox_options”錯誤
