简体   繁体   English

单击一个按钮在 python selenium 中显示一个文本框

[英]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以下是按钮 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 -尝试在您的代码中使用 ActionChains class,如下所示 -

# 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 :要单击元素,您需要为element_to_be_clickable()诱导WebDriverWait ,您可以使用以下任一Locator Strategies

  • Using CSS_SELECTOR :使用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 :使用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.必须删除我想出现的字段的样式属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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