How get id from dynamic button ? (Python) - python

I have this xpath element who change dynamically, and i like to click on it, how i can do it?
//*[#id="/api/services/61_ellipsis"]
The button HTML is:
<button type="button" class="v-btn v-btn--flat v-btn--icon v-btn--round theme--light v-size--default" id="/api/services/33_ellipsis">
<span class="v-btn__content">
<i aria-hidden="true" class="v-icon notranslate mdi mdi-dots-vertical theme--light">
</i>
</span>
</button>

The desired element is a dynamic element so to click() on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following solutions:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.v-btn.v-btn--flat.v-btn--icon.v-btn--round.theme--light.v-size--default[id^='/api/services/'] > span.v-btn__content > i.v-icon.notranslate.mdi.mdi-dots-vertical.theme--light"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='v-btn v-btn--flat v-btn--icon v-btn--round theme--light v-size--default' and starts-with(#id, '/api/services/')]/span[#class='v-btn__content']/i[#class='v-icon notranslate mdi mdi-dots-vertical theme--light']"))).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

If the ID changes dynamically, you cannot use the exact ID to get the element, but a contains query might work
Query on any element that contains api/services in the ID:
//*[contains(#id, 'api/services')]
Query on the button without any ID info. Has inner span and i elements:
//button[span/i]
Query on button element that contains api/services in the ID, and also has inner span and i elements:
//button[span/i and contains(#id, 'api/services')]
To save the ID, you want to find the element then get it's ID attribute:
buttonId = driver.find_element_by_xpath("//button[span/i and contains(#id, 'api/services')]").get_attribute("id")

Related

Selenium: How to click on a link with no class or ID

How do I click this link using Selenium in Python? I've been able to get to the webpage using Selenium and click other links that have IDs. But this one doesn't have an ID or name.
HTML:
<div ng-repeat="dashboard in $ctrl.dashboards track by $index" class="btn btn-primary ng-scope active" ng-class="{active: dashboard.Dashboard_Id == $ctrl.activeDashboard.Dashboard_Id}" ng-click="$ctrl.dashboardClick(dashboard)" style="">
<span ng-bind="dashboard.Name" class="ng-binding">Hours By Activity</span>
</div>
I've tried this with no luck:
driver.find_elements_by_xpath("//*[contains(text(), 'Hours by Activity')]")
The desired element is an Angular element so to click on the clickable 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, "span.ng-binding[ng-bind='dashboard.Name']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[#class='ng-binding' and #ng-bind='dashboard.Name'][text()='Hours By Activity']"))).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

How to find the value of all the id attribute of all the button elements in a web page using Selenium

Suppose there are multiple buttons in a web page
<button id='xyz'></button>
<button id='abc'></button>
<button id='pqr'></button>
I want the value of the id attributes of all the buttons using selenium
I am using this code
for button in driver.find_elements(by=By.XPATH,value='//button'):
print(button.get_attribute('id'))
I am not getting any thing in the output
Considering the HTML
<button id='xyz'></button>
<button id='abc'></button>
<button id='pqr'></button>
To extract the value of the id attributes of the <button> elements you have to induce WebDriverWait for visibility_of_all_elements_located() and using List Comprehension you can use either of the following locator strategies:
Using TAG_NAME:
print([my_elem.get_attribute("id") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.TAG_NAME, "button")))])
Using CSS_SELECTOR:
print([my_elem.get_attribute("id") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "button")))])
Using XPATH:
print([my_elem.get_attribute("id") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//button")))])
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

How to click on the ember.js enabled button using Selenium and Python

I have been trying to make this clickable and I just cannot understand what I am doing wrong.
I am also trying to induce webdriverwait, so that it is clicked when it appears.
This is my code so far:
def order(k):
driver = webdriver.Chrome(os.getcwd()+"\\webdriver\\chromedriver.exe")
driver.get("website.com/login-to-checkout")
driver.find_element_by_id('i0116').send_keys(k["email"])
driver.find_element_by_id('i0118').send_keys(k["password"])
driver.find_element_by_id('idSIButton9').click()
delay()
#sign in button
driver.find_element_by_id('idSIButton9').click()
#Button below I cant get to be clicked
with webdriver.Chrome() as driver:
wait = WebDriverWait(driver, 7)
wait.until(presence_of_element_located((By.CSS_SELECTOR, "#ember1053")))
driver.find_element(By.id, "ember1053").click()
this is the source code for the button that I am trying to make clickable:
<div id="ember1037" class="btn-group m-b-lg m-t-lg order-call-to-action ember-view"><!----> <!--biBehavior 80 means place order Place Order-->
<button aria-live="polite" type="button" tabindex="0" data-m="{"aN":"shoppingCart","cN":"PlaceOrder","bhvr":80}" id="ember1053" class="btn theme-default btn-primary cli-purchase ember-view"><!----> Place order
</button></div>
The desired element is an Ember.js element and the value of the id attribute of the <button> will keep changing dynamically, every time you access the AUT(Application Under Test). Hence to click() on the element you have 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.theme-default.btn-primary.cli-purchase.ember-view[id^='ember'][type='button'][aria-live='polite']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn theme-default btn-primary cli-purchase ember-view' and starts-with(#id,'ember')][contains(., 'Place order') and #aria-live='polite']"))).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
References
You can find a couple of relevant detailed discussions in:
Selenium - Finding element based on ember
Ember dropdown selenium xpath
This may help but I've had issues with webdriver not clicking on a button when I use the id to find it.
The work around I've found is using the xpath instead of the id.
Like this, it's worth a try.
driver.find_element_by_xpath("""//*[#id="submit-button"]""").click()

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.

How to find an element with respect to the user input using Selenium and Python?

The following is the HTML structure:
<div class='list'>
<div>
<p class='code'>12345</p>
<p class='name'>abc</p>
</div>
<div>
<p class='code'>23456</p>
<p class='name'>bcd</p>
</div>
</div>
And there is a config.py for user input. If the user input 23456 to config.code, how can the selenium python select the second object? I am using find_by_css_selector() to locate and select the object, but it can only select the first object, which is Code='12345'. I tried to use find_by_link_text(), but it is a <p> element not <a> element. Anyone can help.....
To locate the element with respect to the input by the user using Selenium and python you need to to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
Using variable in XPATH:
user_input = '23456'
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='list']//div/p[#class='code' and text()='" +user_input+ "']")))
Using %s in XPATH:
user_input = '23456'
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='list']//div/p[#class='code' and text()='%s']"% str(user_input))))
Using format() in XPATH:
user_input = '23456'
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='list']//div/p[#class='code' and text()='{}']".format(str(user_input)))))
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
Try the below xpath:
code = '23456'
element = driver.find_element_by_xpath("//p[#class='code' and text()='" +code +"']")

Categories

Resources