在下拉菜单中选择动态元素,Selenium Python

Selecting Dynamic Element in Drop Down Menu, Selenium Python

我正在尝试 select 下拉菜单中的输入框。但是,输入框本身似乎是动态的。我浏览过类似的帖子,但它们似乎是针对特定问题的。我在使用动态元素时遇到过几次这个问题,我希望了解使用动态元素的一般方法。

以下是我要查找的元素的详细信息select:

<input class="lui-search__input ng-pristine ng-valid ng-empty ng-valid-maxlength ng-touched" maxlength="5000" q-placeholder="Object.Listbox.Search" ng-model="query" ng-disabled="disabled" autocomplete="" spellcheck="false" ng-trim="false" type="text" qva-blur="blurred()" qva-focus="autoFocus" qv-escape="escape($event)" qv-enter="enter($event)" placeholder="Search in listbox" aria-invalid="false" xpath="1">

(如果这不是有用的信息,请告诉我,我会更新)。

相对 xpath 变化:

//body/div[8]/div[1]/div[1]/div[1]/ng-transclude[1]/div[1]/div[3]/div[1]/article[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/input[1]

在一个实例中

相对 xpath 的另一个实例:

//body/div[7]/div[1]/div[1]/div[1]/ng-transclude[1]/div[1]/div[3]/div[1]/article[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/input[1]

我曾尝试通过 classcss selector select,但没有成功。我真的很想了解如何解决这个具体问题,以及我应该在元素中的哪个位置使用以供将来使用动态元素的一般概念。

谢谢!

(如果我提供的元素详细信息没有帮助,也可以使用以下代码访问该元素:)

driver.get("https://bi.prozorro.org/sense/app/fba3f2f2-cf55-40a0-a79f-b74f5ce947c2/sheet/NFTrm/state/analysis")

driver.find_element_by_xpath("//thead/tr[1]/th[2]").click()

#the input box I am attempting to select to no avail:
while True:
        try:
            WebDriverWait(driver, 25).until(EC.presence_of_element_located((By.XPATH, "//body/div[7]/div[1]/div[1]/div[1]/ng-transclude[1]/div[1]/div[3]/div[1]/article[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/input[1]")))
            break
        except TimeoutException:
            print("Loading took too much time!")

您可以使用它的属性之一来找到它。

XPATH:

//input[@placeholder='Search in listbox']

is an Angular element. So ideally to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following :

  • 使用XPATH:

    driver.get('https://bi.prozorro.org/sense/app/fba3f2f2-cf55-40a0-a79f-b74f5ce947c2/sheet/NFTrm/state/analysis')
    WebDriverWait(driver, 60).until(EC.element_to_be_clickable((By.XPATH, "//th[@tid='st.header']//span[@title='Учасник']//following::th[@tid='st.header.search']"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@placeholder='Search in listbox']"))).click()
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • 浏览器快照: