Home > Blockchain >  How to click on this element using Selenium?
How to click on this element using Selenium?

Time:08-22

I have to click on an element that has several of the same, the way to differentiate is by the "onclick" attribute but I can't click on it. How would xpath be?

<button onclick="VxManager.getWidget('DirectQuoteLineItemList_inner').filterApply('4', 'DirectQuoteLineItemList_inner_COL_4');" id="Button" type="button" title="OK" >OK</button>

I think that is something like this:

WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="Button"][@title="OK"][@onclick="VxManager.getWidget('DirectQuoteLineItemList_inner').filterApply('4', 'DirectQuoteLineItemList_inner_COL_4');"]))).click()

But this doesn't work and in the page has others titles, ids and classes.

Thank you in advance.

CodePudding user response:

We can't give you 100% correct answer before seeing the page you are working on, but I can guess it could be something like this:

WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,'//button[@id="Button"][@title="OK"][contains(@onclick,"DirectQuoteLineItemList_inner")]))).click() 

I guess the DirectQuoteLineItemList_inner part there should be fixed value.
Or maybe this, as suggested by JeffC

WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,'//button[@id="Button"][@title="OK"][contains(@onclick,"DirectQuoteLineItemList_inner_COL_4")]))).click() 

CodePudding user response:

As you mentioned, the only way to differentiate is by the onclick attribute you can use it exclusively.

However as the desired element is a dynamic element, so to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.VButton.default-true#Button[onclick*='VxManager'][onclick*='DirectQuoteLineItemList_inner']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(@onclick, 'VxManager') and contains(@onclick, 'DirectQuoteLineItemList_inner')][@class='VButton  default-true' and @id='Button']"))).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
    
  • Related