1

I'm developing test automation and I want to click on element located in toolbar,I'm using python and selenium. Here is a code of website

<dl class="ToolbarDropdown_menu_2wY9X ToolbarDropdown_isHidden_3UaGr" tabindex="0" style="" 
xpath="1"><dt><span class="ToolbarDropdown_title_1NBVn">New</span>
<span class="DropdownArrow_arrowIcon_dDzph DropdownArrow_down_3dlvo"></span>
</dt>
<dd class="ToolbarDropdown_item_nP-_M" style="">Dataset</dd>
<dd class="ToolbarDropdown_item_nP-_M">Project</dd>
</dl>

I need to click on element with text Dataset.

I locate element in this way

DATASET_BUTTON = (By.XPATH, '//dd[contains(text(),"Dataset")]')

And I want to perform action like that

self.driver.find_element(*VideosPageLocator.DATASET_BUTTON).click()

And there is no action but in terminal looks like my step has passed. Do you have any idea how to click on element in dropdown?

Guy
  • 46,488
  • 10
  • 44
  • 88
user3388384
  • 233
  • 3
  • 12
  • I have seen this with applications that use Angular to load the DOM. The click happens successfully but Angular is still loading the DOM and "ignores" the click. Put `time.sleep(3)` before the click. If that works build a custom function to verify the click resulted in the expected changes to the page. – Jortega Feb 20 '20 at 14:31

2 Answers2

2

Maybe the action is done before DOM has loaded completely.
Try using WebDriverWait.

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

web_driver_wait = WebDriverWait(self.driver, 10)
element = web_driver_wait.until(EC.element_to_be_clickable((By.XPATH,'//dd[contains(text(),"Dataset")]')))
element.click()
0xWise
  • 79
  • 1
  • 6
0

To invoke click() on the element with text as Dataset you have to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategy:

  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//dl[starts-with(@class, 'ToolbarDropdown')]//dd[starts-with(@class, 'ToolbarDropdown_item') and text()='Dataset']"))).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