简体   繁体   中英

Click a button to display a text box in python selenium

I am trying to click a button which un-hides another text box. The button click changes the script from

<span id="incident.u_brand_edit" style="display: none;">

to

<span id="incident.u_brand_edit" style>

Following is the button HTML

<button class="btn btn-default btn-ref" type="button" aria-labelledby="incident.u_brand_title_text" 
data-target="#incident\.u_brand" title="" tabindex="0" id="incident.u_brand_unlock" 
data-type="glide_list_unlock" data-ref="incident.u_brand" data-auto-close="false" 
data-placement="auto" style="margin-right: 5px;" list-read-only="false" 
data-original-title="Unlock Brand"><span class="icon icon-locked" aria-hidden="true"></span></button>

I am trying to achieve this using the following code

driver.find_element_by_id("incident.u_brand_unlock").click()

Also tried this

driver.find_element_by_id("incident.u_brand_unlock").send_keys("\n")

The above codes are focusing on the button but it's not clicking and the text box is not unhiding to perform further operations.

Try to use the ActionChains class in your code like below -

# Import
from selenium.webdriver import ActionChains

wait = WebDriverWait(driver, 5)
action = ActionChains(driver)

Button_Click = wait.until(EC.element_to_be_clickable((By.ID, 'incident.u_brand_unlock')))
action.move_to_element(Button_Click).click().perform()

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.btn.btn-default.btn-ref[id$='u_brand_unlock'][data-original-title='Unlock Brand']>span"))).click()
  • Using XPATH :

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='btn btn-default btn-ref' and contains(@id, 'u_brand_unlock')][@data-original-title='Unlock Brand']/span"))).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

Found out the answer in this post . Had to remove the style attribute of the field I wanted to appear.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM