1

So am trying to figure out how to get the right cssSelector to access an html element from Python web driver for selenium.

I have a page where two drop down option are there. I want to select the one which shows "Fast Mode" and then select the second option in that drop down using Python web driver.

enter image description here

A similar drop down on left also has similar element

<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>

How do I find the right cssSelector as class names appear same.

There is a data-dialog-id which seems to have diff values but am not sure which method in web driver can help me use that.

My code to access elements as follows:

driver = webdriver.Chrome()
toggle_button=driver.find_element_by_css_selector('a[data-dialog-id="]')
toggle_button.click()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Baktaawar
  • 7,086
  • 24
  • 81
  • 149

3 Answers3

0

your screen shot does not show any of the options that would appear after you select "Fast Mode", so it is difficult to provide you with any help choosing that. However, "Fast Mode" does have a unique class, "dropdown-toggle-search-mode"

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()

I'm avoiding that data-dialog-id attribute just because I'm suspicious that it won't be consistent with each product build, but if you know for sure that there will always be a one to one association, you could use it, but ONLY after you click the link that brings it up (that fast_mode_button).

Breaks Software
  • 1,721
  • 1
  • 10
  • 15
0

To click() on the element with text as Job you have to induce WebDriverWait for the element to be clickable and you can use either of the following Locator Strategies:

  • Using PARTIAL_LINK_TEXT:

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

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='btn-pill dropdown-toggle active' and contains(., 'Job')]"))).click()
    
  • Using 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()
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
-1

i have done one automation project in java selenium where i have perform actions on drop downs

//in page objects

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;
    }

//In Test file

    {        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);
    }
akshay patil
  • 670
  • 7
  • 20