How to find element using Selenium and Python - python

Just starting to learn selenium using python and running into trouble with the basics of finding elements. This is a search/text box that I am trying to click into and send text to it.
Here is the code of the element I got from Edge.
<div class="box" id="ClinicSrch" style="display:block">
<span align="lb">Last:</span>
<input type="search" name="NameLast" size="15" class="white1" value="" maxlength="25">
First: <input type="search" name="NameFirst" size="15" class="white1" value="" maxlength="25"><br>
Here is my code:
driver.find_element(By.XPATH, '//*[#id="ClinicSrch"]/input[1]').click()
The xpath that I get when copying the element from edge inspect tool is:
//*[#id="ClinicSrch"]/input[1]
Full xpath is:
/html/body/form/div[1]/div[1]/input[1]
I've been trying with other fields and buttons where I have similar xpaths but nothing is working. I always get no such element.

To locate the Last Name search field you can use either of the following locator strategies:
Using css_selector:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#PtSrchFrame[name='PtSrchFrame']")))
element = driver.find_element(By.CSS_SELECTOR, "input[name='NameLast']")
Using xpath:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='PtSrchFrame' and #name='PtSrchFrame']")))
element = driver.find_element(By.XPATH, "//input[#name='NameLast']")
Ideally, to locate 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.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#PtSrchFrame[name='PtSrchFrame']")))
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='NameLast']")))
Using XPATH:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='PtSrchFrame' and #name='PtSrchFrame']")))
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='NameLast']")))
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

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 an element by title with Selenium Python

I'm trying to check a checkbox with selenium and the only available info is the title.
Is a dynamic element, so I need to select by the title.
I tried all the methods without success (contains #title, etc).
Can someone please help? I'm beginning to code.
The xpath is:
/html/body/div[7]/div[2]/div/div[2]/div/div[2]/div/div/div[1]/form/div/span[1]/input
The CSS Selector is:
#selectCommon > span:nth-child(1) > input:nth-child(1)
The HTML looks like:
<input type="checkbox" title="AM - AMERICAS">
To click() on the element with title as AM - AMERICAS you can use either of the following locator strategies:
Using css_selector:
driver.find_element(By.CSS_SELECTOR, "input[title^='AM'][title$='AMERICAS']").click()
Using xpath:
driver.find_element(By.XPATH, "//input[starts-with(#title, 'AM') and contains(#title, 'AMERICAS')]").click()
Ideally 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, "input[title^='AM'][title$='AMERICAS']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[starts-with(#title, 'AM') and contains(#title, 'AMERICAS')]"))).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 do I identify the element by the class with Selenium and Python?

<input class="inputDefault-_djjkz input-cIJ7To" name="password" type="password" placeholder="" aria-label="Password" autocomplete="off" maxlength="999" spellcheck="false" value="">```
^this is the HTML
ive been trying something like
login = driver.find_element_by_xpath(".//*[#class='inputDefault-_djjkz input-cIJ7To']/div/input").click()
thanks for any help
You can use the following code :
driver.find_element_by_xpath("//input[#class='inputDefault-_djjkz input-cIJ7To']")
To identify the clickable element you can use either of the following Locator Strategies:
Using css_selector:
element = driver.find_element(By.CSS_SELECTOR, "input[name='password'][aria-label='Password']")
Using xpath:
element = driver.find_element(By.XPATH, "//input[#name='password' and #aria-label='Password']")
Ideally, to identify 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:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='password'][aria-label='Password']")))
Using XPATH:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='password' and #aria-label='Password']")))
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

Locate element without any name or class using Selenium and Python

I'm using Selenium and I want to grab the 'a' tag so I can navigate into my user profile. What is the best way to do this?
Here is the HTML:
<div class="parent-cont">
<div class="akd3 dafk4 dfan4">...</div>
<div class="avndkd dakdf">...</div>
<div class="fjkad fdadj dfakees">
<a aria-label="tag" class="oa2 g5iad jhuo" href="/profile.php?id=1792" role="link" tabindex="0">
<div class="dsks dssks">...</div>
<div class="dka dk2 fdakdd">...</div>
</a>
</div>
</div>
To locate the element you can use either of the following Locator Strategies:
Using css_selector:
element = driver.find_element_by_css_selector("a[aria-label='tag'][href='/profile.php?id=1792']")
Using xpath:
element = driver.find_element_by_xpath("//a[#aria-label='tag' and #href='/profile.php?id=1792']")
Ideally, to locate the element you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a[aria-label='tag'][href='/profile.php?id=1792']")))
Using XPATH:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[#aria-label='tag' and #href='/profile.php?id=1792']")))
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
you can use following code as mentioned:
enter code here
a = driver.find_elements_by_tag_name("a")
for a in x:
print(x.get_attribute('arial-label')

How to click on an element through Selenium ActionChain and Python

I am new to Python Selenium. I am stuck with this. Please help me in finding a solution.
I am trying to click this MENU1 using ActionChain.
locator = (By.XPATH, "//div[#title='MENU1']")
text_element = WebDriverWait(driver, 20).until(visibility_of_element_located(locator))
actions = ActionChains(driver)
actions.move_to_element(text_element).click().perform()
Below is the HTML:
<div _ngcontent-c0="" class="hyd-group-tree-node-label active" tooltipposition="bottom" title="MENU1">
<span _ngcontent-c0="" class="ui-treenode-inner-icon fa fa-fw fa-building"></span>
MENU1
</div>
Everything runs fine. No errors.
It moves to the element but I am not able to see the click movement.
I am not sure what I am missing. I tried with text_element.click() but it is not working. I tried in both Chrome and Firefox.
Thank you
The desired element is a Angular element so invoke click() you need to induce WebDriverWait for the desired 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, "div.hyd-group-tree-node-label.active[title='MENU1']>span.ui-treenode-inner-icon.fa.fa-fw.fa-building"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='hyd-group-tree-node-label active' and #title='MENU1']/span[#class='ui-treenode-inner-icon fa fa-fw fa-building']"))).click()
If you still want to use ActionChains you can use either of the following solutions:
Using CSS_SELECTOR:
ActionChains(driver).move_to_element(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.hyd-group-tree-node-label.active[title='MENU1']>span.ui-treenode-inner-icon.fa.fa-fw.fa-building")))).click().perform()
Using XPATH:
ActionChains(driver).move_to_element(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='hyd-group-tree-node-label active' and #title='MENU1']/span[#class='ui-treenode-inner-icon fa fa-fw fa-building']")))).click().perform()
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

Categories

Resources