Selenium Python 如何找到//两个值之间的跨度数

Selenium Python how to find //span number between two values

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>

在 selenium chrome 网络驱动程序中。我试图找到数字范围 (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()

请指教

你可以试试: 而 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.    
     

如果您遇到任何问题,请在下方评论。

您可以将所有具有 XPath //span[text()] 的 Web 元素存储在如下列表中,然后使用循环进行迭代并放置条件 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)
driver.find_element(By.XPATH,"//div[@class='lend-count']/span[number()>10 and number()<20]").click()

只需使用 number()>10 和 number()<20 即可获取范围内的数字。要确保它出现,请使用 webdriver waits。

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