Xpath creation for Selenium for clicking a dropdown menu - python

I m trying to select a dropdown arrow menu after Status.
I'm trying to build a Xpath to click this :
<a href="javascript:" class="btn btn3d selectionbtn" style="top:0px; left:136px; width:21px; height:21px;">
Xpath = driver.find_element_by_xpath('//*[id="WIN_3_7"]/div/a').click()
But it says that the
Unable to locate an element
Below is the HTML snippet of the above image.
<div id="WIN_3_7" arid="7" artype="EnumSel" ardbn="Status" arlbox="0,4,104,17" class="df arfid7 ardbnStatus EnumSel" style="z-index:1928;top:173px; left:705px; width:262px; height:21px;" arwindowid="3">
<label id="label7" class="label f9" for="x-arid_WIN_3_7" style="top:4px; left:0px; width:104px; height:17px;">Status*</label><div class="selection" style="top:0px; left:105px; width:157px; height:21px;" arselmenu="[{v:"Assigned"},{v:"Pending"},{v:"Waiting Approval"},{v:"Planning"},{v:"In Progress"},{v:"Completed"},{v:"Rejected"},{v:"Cancelled",l:"Canceled"},{v:"Closed"}]">
<input id="arid_WIN_3_7" type="text" class="text " readonly="" style="top:0px; left:0px; width:136px; height:21px;" title="Assigned">
<a href="javascript:" class="btn btn3d selectionbtn" style="top:0px; left:136px; width:21px; height:21px;">
<img class="btnimg" src="../../../../resources/images/mt_sprites.gif" alt="">
</a>
</div>
</div>

To click and expand the dropdown menu you can use the following line of code :
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#id="WIN_3_7"]/div[#class='selection']/a[#class='btn btn3d selectionbtn']/img[#class='btnimg']"))).click()
Update
As an alternative you can also try to click() on the <input> tag and expand the dropdown menu as follows :
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='text' and #id='arid_WIN_3_7']"))).click()

Related

Unable to access the button in Selenium Python using Xpath

Error Msg:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[text()="Cancel"]"}
I have tried with tagname as button too.
The HTML for this:
<form name="forgotPassword" id="forgotPassForm" action="https://login.salesforce.com/secur/forgotpassword.jsp"
autocomplete="off" method="post" target="_top" novalidate="novalidate">
<p class="username">To reset your password, enter your Salesforce username.</p>
<input type="hidden" name="locale" value="in" />
<div class="verifyform">
<label for="un" class="label">Username</label>
<input id="un" type="email" class="input wide mb12 mt8 username" onKeyPress="checkCaps(event)"
name="un" value="" autofocus />
<input type="submit" name="continue" id="continue" class="button primary fiftyfifty right focus" value="Continue" />
<input type="button" name="cancel" onclick="parent.location='/?locale=in'" class="secondary button fiftyfifty mb16" value="Cancel" >
</div>
<input type="hidden" name="lqs" value="locale=in" />
<div id='pwcaps' class='pwcaps' style='display:none'>Caps Lock is on. Please try to log in again and remember that passwords are case-sensitive.</div>
<a class="hiddenlink" href="javascript:document.forgotPassword.submit();" id="continueReset">Continue</a>
<p class="small">Video: Need Help Logging In?</p>
</form>
My code:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(service=Service("C:\\Users\\Dell\\Desktop\\Selenium\\chromedriver.exe"))
driver.maximize_window()
driver.get("https://login.salesforce.com/")
driver.find_element(By.CSS_SELECTOR, '#username').send_keys('Tac')
driver.find_element(By.CSS_SELECTOR, '.password').send_keys('PASSWRd')
driver.find_element(By.CSS_SELECTOR, '.password').clear()
driver.find_element(By.LINK_TEXT, 'Forgot Your Password?').click()
driver.find_element(By.XPATH, '//button[text()="Cancel"]').click()
The element Cancel doesn't have a innerText rather the value attribute is set as Cancel
<input type="button" name="cancel" onclick="parent.location='/'" class="secondary button fiftyfifty mb16" value="Cancel">
Solution
To click element instead of presence_of_element_located() 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[value='Cancel']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#value='Cancel']"))).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
Have you tried the following code:
driver.find_element(By.XPATH, '//*[#id="forgotPassForm"]/div[1]/input[3]').click()
To get the basic XPath you can inspect element by right clicking on the web page and selecting 'Elements'. You can then right click the button and then 'Copy > Copy XPath'. This will allow you to get the very basics working before then refactoring and using a more robust piece of xcode.
See screenshot:
The element you are trying to access is input element NOT button element.
Use following xpath to identify the element.
driver.find_element(By.XPATH, "//input[#name='cancel' and #value='Cancel']").click()
To handle dynamic element use WebDriverWait() and wait for element to be clickable.
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='cancel' and #value='Cancel']"))).click()
You need to import below libraries.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

Python Selenium input date in ng datepicker

I am trying to input a date into the text portion of a datepicker and send_keys is not working.
Here is the html:
<date-picker _ngcontent-bdp-c11="" _nghost-bdp-c3="">
<div _ngcontent-bdp-c3="" class="datePicker formDiv-header datePickerContainer">
<div _ngcontent-bdp-c3="" class="formLabel-header">
<!---->
<label _ngcontent-bdp-c3="" for="dateInput1">Search Date:</label>
<!---->
</div>
<div _ngcontent-bdp-c3="" class="formField-header" style="padding:0;">
<div _ngcontent-bdp-c3="" class="input-group date" id="dateTimePicker1">
<input _ngcontent-bdp-c3="" class="form-control formField-header" maxlength="10" type="text">
<span _ngcontent-bdp-c3="" class="input-group-addon"><span _ngcontent-bdp-c3="" class="glyphicon glyphicon-calendar">
</span>
</span>
</div>
</div>
</div>
</date-picker>
<search-button _ngcontent-bdp-c11="" _nghost-bdp-c4="">
<button _ngcontent-bdp-c4="" class="selectionButton" type="button"> Search
</button></search-button>
Here is my code:
dateInputfield = driver.find_element_by_xpath('// *[ # id = "dateTimePicker1"] / input')
# the dateInputfield: <selenium.webdriver.remote.webelement.WebElement (session="6911b2bc422dac02d926b3b36429e8de", element="0.8362094047920967-3")>
dateInputfield.send_keys('01/15/2021')
searchbt = driver.find_element_by_xpath('//*[#id="filters"]/search-button/button')
searchbt.click()
Alternatively, I have tried to click the calendar icon, which actually opens the calendar for selection, but I don't know how to navigate to a given date in order to select it.
calicon = driver.find_element_by_xpath('//*[#id="dateTimePicker1"]/span/span')
calicon.click()
Any help would be appreciated.
To send the date characters to the element you can use either of the following Locator Strategies:
Using css_selector:
driver.find_element(By.CSS_SELECTOR, "div.input-group.date#dateTimePicker1 > input.form-control.formField-header").send_keys('01/15/2021')
Using xpath:
driver.find_element(By.XPATH, "//div[#class='input-group date' and #id='dateTimePicker1']/input[#class='form-control formField-header']").send_keys('01/15/2021')
Ideally, to send the date characters to 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, "div.input-group.date#dateTimePicker1 > input.form-control.formField-header"))).send_keys('01/15/2021')
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='input-group date' and #id='dateTimePicker1']/input[#class='form-control formField-header']"))).send_keys('01/15/2021')
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

Selenium WebDriver “find_element_by_xpath” on WebElement

<figcaption class="ncss-row">
<div class="ncss-col-sm-12 full">
<div class="figcaption-content">
<div class="copy-container ta-sm-c bg-white pt6-sm pb7-sm pb5-lg">
<h3 class="ncss-brand u-uppercase text-color-grey mb-1-sm mb0-md mb-3-lg fs12-sm fs14-md">
Air Jordan XI<!-- -->
</h3>
<h6 class="ncss-brand u-uppercase fs20-sm fs24-md fs28-lg">Black/Red<!-- -->
</h6>
</div>
<div data-qa="shop-now-cta" class="cta-container bg-white pt6-sm pb7-sm pb5-lg prl12-sm pb8-sm
pt8-lg ta-sm-c xh-highlight">
<div>
<button class="cta-btn u-uppercase ncss-btn text-color-white ncss-brand d-sm-b d-lg-ib pr5-
sm pl5-sm pt3-sm pb3-sm d-sm-ib bg-black test-buyable buyable-full-width buyable-feed"
data-qa="notify-me-cta theme-feed">通知我
</button>
</div>
</div>
</div>
</div>
</figcaption>
I want to locate the button by find_element_xpath, bu it always always appears
[<selenium.webdriver.remote.webelement.WebElement (session="10bf8a8d83d1b36188a6b1e41856c052", element="0.5294218443885808-2")>]
Any help would be greatly appreciated.
url = 'https://www.nike.com/cn/launch/'
browser = webdriver.Chrome()
browser.get(url)
elements = browser.find_element_by_xpath('//figure/div/div/figcaption/div/div/div[2]/div/button').text
print(elements)
To Click on the Button Induce WebDriverWait and wait for element_to_be_clickable() And use following Css selector.
WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'div[data-qa="shop-now-cta"] button[data-qa="notify-me-cta theme-feed"]'))).click()
You need to import following libraries.
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
Edited
To get the Text of the button use below code.use get_attribute("textContent")
Or get_attribute("innerHTML") since the item text is hidden on the page.
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium import webdriver
browser=webdriver.Chrome()
browser.get("https://www.nike.com/cn/launch/")
print(WebDriverWait(browser,10).until(EC.presence_of_element_located((By.CSS_SELECTOR,'div[data-qa="shop-now-cta"] button[data-qa="notify-me-cta theme-feed"]'))).get_attribute("textContent"))

How to click available button which refuses the click?

I'm working on automation of one website. My problem is that the button which is visible in my selenium (simply prints out) stopped working after recent update. Eventho I can see it, I cannot click it. I've noticed that it has started to have dynamic id selector, but class stays stable. What that can be ? Is there any other way to click it ?
<div>
<div>
<div class="pull-left middle-col-4">
<!---->
</div>
<div class="pull-left middle-col-4">
<!---->
</div>
</div>
<button tabindex="-1" id="exit-button-ZpyYaHCdmZ5jnmaamGhjaJjFcsVrmJOUcZWVaZlsaGlolpOaZg" class="btn btn-inverse btn-large pull-right">Wyjście</button>
</div>
To Click on the button induce WebDriverWait and element_to_be_clickable() And following locator strategy.
By Css Selector:
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"button.btn.btn-inverse.btn-large.pull-right"))).click()
By Xpath:
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//button[#class='btn btn-inverse btn-large pull-right' and starts-with(#id,'exit-button-')][text()='Wyjście']"))).click()
You need to import followings to execute above code.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Please find below solution to click exit button if id is dynamic
1. Xpath and contains method
button=driver.find_element_by_xpath("//button[contains(text(), 'Wyjście')]")
button.click()
2. Class name
element = driver.find_element_by_class_name("btn btn-inverse btn-large pull-right")
element.click()

How to click on a Radio Button as per the HTML using Selenium and Python

I am trying to click on element with following html code:
<div class="delivery-pseudo-table">
<div class=" i-popup-lk user-address" id="userAddress">
<div class="address-container">
<div class="titlebar">
<h1 class="title"></h1>
</div>
<div id="userAddressContent"></div>
</div>
<div class="hide j-map map" id="addressMap"></div>
<div class="hide j-map-pickpoint postamat-map" id="pickpointMap"></div>
<div class="added-addres-loader"></div>
</div>
<ul class="delivery-method" data-jsv-df=""><li class="selfDelivery selectDeliveryWay active" data-jsv="#34_#35_" title="">
<script type="jsv#37_"></script>
<label><input autocomplete="off" checked="checked" data-id="2" name="orderDetails.DeliveryWay_q" type="radio" value="Self"/>Самовывоз</label>
<script type="jsv/37_"></script>
</li><li class="courierDelivery selectDeliveryWay" data-jsv="/35_#38_" title="">
<script type="jsv#39_"></script>
<label><input autocomplete="off" data-id="1" name="orderDetails.DeliveryWay_q" type="radio" value="Courier"/>Доставка курьером</label>
<script type="jsv/39_"></script>
</li><li class="wbpostamatDelivery selectDeliveryWay" data-jsv="/38_#40_" title="">
<script type="jsv#41_"></script>
<label><input autocomplete="off" data-id="16" name="orderDetails.DeliveryWay_q" type="radio" value="WbPostamat"/>Постамат</label>
<script type="jsv/41_"></script>
</li></ul>
I need to click on button with value "Courier"
And I am trying to do it with following Python code:
elem = browser.find_element_by_css_selector("input[type='radio'][#value='Courier']")
elem.click()
(I've also tried to find by xpath)
But it says that the element is invisible. Are the any ways to click on such element?
Try to use this xPath:
//input[#type='radio' and #value='Courier']
also try to use WebDriverWait:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[#type='radio' and #value='Courier']"))).click()
Note: you have to add some imports:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
WebDriverWait statement is for waiting at least 10 seconds until element will be clickable and only then clicks on it.
PS: more information about WebDriverWait can be found here.
EDIT: try to execute JavaScript:
radio_btn = driver.find_element_by_xpath("//input[#type='radio' and #value='Courier']")
driver.execute_script("arguments[0].click();", radio_btn)
As per the HTML you have shared the <ul> tag contains a lot of JavaScripts so to invoke click() on the desired element you have to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:
CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "ul.delivery-method li.courierDelivery.selectDeliveryWay input[value='Courier']"))).click()
XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ul[#class='delivery-method']//li[#class='courierDelivery selectDeliveryWay']//input[#value='Courier']"))).click()

Categories