Home > Enterprise >  Interacting with buttons in a panel using Python - Selenium/Chrome Driver
Interacting with buttons in a panel using Python - Selenium/Chrome Driver

Time:09-09

I need to click on a button in a pop-up panel, select an option from its drop-down menu, read that option's text (just an asset ID), and then select a button to Generate a report. For some reason, I am able to access the panel's text area to input my custom file name, but cannot interact with the buttons. An image of the panel in question is linked below for reference.

The text area's XPATH:

/html/body/div[4]/div[2]/div/form/div/md-content/md-tabs/md-tabs-content-wrapper/md-tab-content[2]/div/md-content/div[2]/textarea

The code that will access the text area, clear the default name, and send the new file name:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[3]/div[2]/div/form/div/md-content/md-tabs/md-tabs-content-wrapper/md-tab-content[2]/div/md-content/div[2]/textarea'))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[3]/div[2]/div/form/div/md-content/md-tabs/md-tabs-content-wrapper/md-tab-content[2]/div/md-content/div[2]/textarea'))).clear()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[3]/div[2]/div/form/div/md-content/md-tabs/md-tabs-content-wrapper/md-tab-content[2]/div/md-content/div[2]/textarea'))).send_keys(file_name)

The "Generate" button's XPATH:

/html/body/div[4]/div[2]/div/form/div/md-content/md-tabs/md-tabs-content-wrapper/md-tab-content[2]/div/md-content/div[1]/md-menu-bar/md-menu/button

The code unable to click the "Generate" button:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[4]/div[2]/div/div/button[1]'))).click()

The pop-up panel's XPATH:

/html/body/div[4]/div[2]

Any and all help is appreciated, thank you!

Panel I am interacting with

CodePudding user response:

If the error is that the element is not clickable, but it is being found you can try this.

#Wait until the element is loaded
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[4]/div[2]/div/div/button[1]')))

#find the element, assuming your webdriver is called driver (change the 'driver' part below if not)
button = driver.find_element(By.XPATH, '/html/body/div[4]/div[2]/div/div/button[1]')

#click the button with a script, again assuming the webdriver name
driver.execute_script('arguments[0].click()', button)

This works by using JavaScript to click the button element rather than the button needing to be clickable at a given position.

If your problem was that the button was not clickable, that may help solve it. Sorry if this didn't help.

  • Related