簡體   English   中英

Selenium function 在 Python 中查找包含部分文本的頁面和元素

[英]Selenium function in Python to find on page and element that contains partial text

我經常收到很多郵件,其中包含一些文件名及其 ID (eg FILENAME_VERSION_ID) 基於這些 ID,我必須登錄門戶並分別下載每個文件。 所以我在 Python 基於 Selenium 做了一個腳本來自動下載這些文件。

我的程序按以下方式工作:

該腳本將這些文件的 ID 提取到一個名為ID.txt的 txt 中。

然后,我使用了一個 for 循環來讀取 ID 文件的每一行,直到它結束。

所以我現在想要的是根據完整文件名中的部分文本(文件名的 id)從 ID.txt 中找到元素。

with open('ID.txt') as f:
for line in f:
    driver.find_element_by_xpath("//*[contains(@id,'%s')]" % str(line))
    pyautogui.press('enter')
    driver.find_element_by_xpath("//*[text()='ro']").click()
    driver.find_element_by_xpath("//*[contains(@id,'%s')]" % str(line)).click()
    driver.find_element_by_xpath("//*[text()='export']").click()
    if 'str' in line:
        break

顯然 selenium 找不到這行代碼的元素

driver.find_element_by_xpath("//*[contains(@id,'%s')]" % str(line))

我要單擊的網站上的一個元素如下所示:

<div index="0" aria-busy="false" aria-checked="false" aria-disabled="false" data-head="true" aria-label="09251561001.09251561001.1.31873860875, folder" aria-selected="false" class="option grid-row" role="option" id=":DOMLT_ELISYS:export:09251561001.09251561001.1.31873860875">
   <div class="name-data icon folder" id="id1027">
      <div class="progressbar" role="progressbar" aria-hidden="true" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100">
         <div class="fill" role="presentation" style="width: 0%;"></div>
         <div class="fill" role="presentation" style="width: 0%;"></div>
      </div>
      <a class="name-text" href="#">
      <span>09251561001.09251561001.1.31873860875</span>
      </a>
   </div>
   <div id="cellId1028" class="date-data"></div>
   <div id="cellId1029" class="size-data"></div>
</div>

在我的ID.txt文件中僅存儲點之后的最后一個數字(在這種特定情況下為 31873860875)。

我嘗試了很多可能性,但它不起作用。 我收到以下錯誤:

raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[contains(@id,'31873860875
')]"}

我在這里沒有做什么? 是否有另一種方法可以在網站上選擇/單擊此元素?

當元素未加載並且您試圖定位它時,這種情況經常發生。 您可以使用等待來解決此問題,以便正確加載 DOM。 https://selenium-python.readthedocs.io/waits.html

以下其中一項將很有用

  • Presence_of_element_located
  • 可見性元素定位
  • Presence_of_all_elements_located
  • element_to_be_clickable

在這種情況下, WebDriverWait應該可以幫助您:

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
# from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.ui import WebDriverWait

botton_to_click = WebDriverWait(driver, 10).until(EC.element_to_be_clickable, ((By.XPATH,"//*[text()='ro']")))
botton_to_click.click()

我相信在程序等待足夠長的時間點擊text()='ro'之后,它將能夠點擊"//*[contains(@id,'%s')]" 如果沒有,您可以對以下XPaths執行相同的botton_to_click操作。

driver.find_element_by_xpath("//*[contains(@id,'%s')]" % str(line)).click()
driver.find_element_by_xpath("//*[text()='export']").click()

你離得夠近了。 文本31873860875位於<span>標記中,是text

因此,要定位您需要為visibility_of_element_located()誘導WebDriverWait的元素,您可以使用以下任一Locator Strategies

  • 使用XPATH

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, ""//a[@class='name-text']/span[contains(.,'%s')]" % str(line)))).click()
  • 注意:您必須添加以下導入:

     from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC

暫無
暫無

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

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