簡體   English   中英

使用Python Selenium訪問下拉元素

[英]Accessing drop down element using Python Selenium

因此,我試圖找出如何獲取正確的cssSelector來從Python Web驅動程序訪問Selenium的html元素。

我有一個頁面,其中有兩個下拉選項。 我想選擇一個顯示“快速模式”的選項,然后使用Python Web驅動程序在下拉菜單中選擇第二個選項。

在此處輸入圖片說明

左側的類似下拉菜單也具有類似的元素

<a class="btn-pill dropdown-toggle active" href="#" data-dialog-id="dialog-view28363">                      <i class="message-indicator icon-info-circle" style=""></i>                     Job<span class="caret"></span>                  </a>

當類名相同時,如何找到正確的cssSelector

有一個data-dialog-id似乎具有diff值,但不確定Web驅動程序中的哪種方法可以幫助我使用它。

我的訪問元素代碼如下:

driver = webdriver.Chrome()
toggle_button=driver.find_element_by_css_selector('a[data-dialog-id="]')
toggle_button.click()

您的屏幕快照未顯示選擇“快速模式”后將出現的任何選項,因此很難為您提供選擇幫助。 但是,“快速模式”確實有一個獨特的類“下拉下拉搜索模式”

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

driver = webdriver.Chrome()
fast_mode_button=driver.find_element_by_css_selector('a.dropdown-toggle-search-mode')
fast_mode_button.click()
# now wait for the menu to open, before clicking on your option
options = WebDriverWait(driver, 10).until(EC.visibility_of_elements_located((By.CSS_SELECTOR, 'css_selector_for_menu_options')))
options[2].click()

我之所以避免使用data-dialog-id屬性,只是因為我懷疑它與每個產品版本都不一致,但是如果您確定總會有一對一的關聯,則可以使用它,但只有在您點擊顯示鏈接的鏈接之后(fast_mode_button)。

我在Java硒中完成了一個自動化項目,在下拉菜單中我執行了一些操作

//在頁面對象中

public static WebElement idProof(WebDriver driver)
    {
        WebElement element=null;
        WebDriverWait wait=new WebDriverWait(driver, 50);
        element=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='panel-body']//div[3]//div[2]//div[1]//a[1]//span[1]")));
        return element;
    }

    public static WebElement idProofVoterId(WebDriver driver, String idVal)
    {
        WebElement element=null;
        WebDriverWait wait=new WebDriverWait(driver, 50);
        element=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//li[contains(text(),'" + idVal +"')]")));
        return element;
    }

//在測試文件中

    {        WebElement idProof = FrmrPage.idProof(driver);
            idProof.click();
            Genlib.sleep(2000);

            WebElement voterId = FrmrPage.idProofVoterId(driver, datArr[8]);
            voterId.click();
            test.pass("ID Proof: " + datArr[8]);
            Genlib.sleep(1000);
    }

要在文本為Job的元素上click() ,必須誘使WebDriverWait使該元素可單擊,並且可以使用以下定位策略之一

  • 使用PARTIAL_LINK_TEXT

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Job"))).click() 
  • 使用XPATH A

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='btn-pill dropdown-toggle active' and contains(., 'Job')]"))).click() 
  • 使用XPATH B

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='btn-pill dropdown-toggle active' and normalize-space()='Job']"))).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