I need some help. Chrome (v 75.0.3770.100) using Selenium Basic ChromeDriver (v 75.0.3770.140) in Excel (2013) VBE. There's an input box which generates a dynamic list if the customer id# exists. I wish to fill in the customer id# then select from the dynamic drop down. But first step, I'm struggling to input my text to the box. I'm able to click on the box with
obj.FindElementById("selectcustTxt").Click
but when I try to fill in the box with:
obj.FindElementById("selectcustTxt").Value = "1111"
I get an error Run-time error '424': Object required
I tried the following FindElementByXPath with both .Value and .Text but get the same Run-time error '424': Object required
obj.FindElementByXPath("//input[#class='form-control cust-autosuggest ng-pristine ng-valid ng-touched'][#id='selectcustTxt']").Value = "1111"
Here's the HTML:
<div class="form-group search-field"><input id="selectcustTxt" type="text" class="form-control cust-autosuggest ng-valid ng-touched ng-dirty ng-valid-parse" autocomplete="off" plshholder="Enter Cust name" autocomplepte="off" ng-model="cust" suggest-type="custService" sh-autosuggest="custAddresses" data-validation="required">
To send a character sequence within the desired element you can use either of the following Locator Strategies:
Using FindElementByCss:
obj.FindElementByCss("input.form-control.cust-autosuggest.ng-valid.ng-touched.ng-dirty.ng-valid-parse#selectcustTxt").SendKeys ("1111")
Using FindElementByXPath:
obj.FindElementByXPath("//input[#class='form-control cust-autosuggest ng-valid ng-touched ng-dirty ng-valid-parse' and #id='selectcustTxt']").SendKeys ("1111")
Reference
You can find a couple of relevant discussions in:
How to send text to some HTML elements?
Need help to fill number into Chrome input box with Selenium
Related
Trying to select an item from a drop down list using a variable.
I'm able to click the drop down list, I'm able to click on the item with explicit text but not using a variable.
Chrome (v 75.0.3770.142) using Selenium Basic ChromeDriver (v 75.0.3770.140) in Excel (2013) VBA.
it works if i use explicit:
Obj.FindElementByXPath("//option[#label='Z01 - Customer Request - Paid']").Click
but doesn't work if using a string variable:
Obj.FindElementByXPath("//option[#label=SomeStringVariable]").Click
Here's the HTML:
<select id="tickboxreason" name="boxreason" class="emergency-reasons form-control ng-touched ng-dirty ng-valid-parse ng-valid ng-valid-required" ng-model="currentCartConfig.boxreason" ng-options="reason.displayValue for reason in reasonList.emergencyOrderReasons | orderBy:'code' track by reason.code" ng-change="updateRDD()" ng-required="currentCartConfig.emergencyFlag" required="required">
<option value="">Select Reason</option>
<option value="Z01" label="Z01 - Customer Request - Paid">Z01 - Customer Request - Paid</option>
</select>
You have missed the string quotes.Try the below option.
Obj.FindElementByXPath("//option[#label='" & SomeStringVariable & "']").Click
I would go with a faster css attribute = value selector
obj.FindElementByCss("[label='" & variable & "']").click
I want to locate and insert value inside a 'value' field.
As you can see the following HTML code the value is empty, I'm trying to locate the element to insert a new value to it but unable to find.
<input aria-label="" id="tab-shared-widget-2" class="" data-tb-test-id="auth-component-password-text-field-TextInput" type="password" value=""
I've tried to locate the element using XPath and by CSS Selector and for some reason my web driver still doesn't find the element using both ways.
susername = driver.find_element_by_css_selector("#tab-shared-widget-2")
susername.send_keys("Something")
spassword = driver.find_element_by_xpath("tab-shared-widget-2").get_attribute('value')
spassword.send_keys("Something")
I am having a UI as given below.
<div class="BtyD1c WFUbU" jsname="Y3PF3">
<div>.........</div>
<div>.........</div>
<div>.........</div>
<div>.........</div>
<div>.........</div>
<div jsname="YCbqLe" style="display: block;">
<div>.........<div>
<div class="qh jxzYFc ZlpBcf XWx4Gf IbyGtb Tea E5 OI LtchOd oj" jscontroller="pxq3x" jsaction="clickonly:KjsqPd; focus:Jt1EX; blur:fpfTEe; input:Lg5SV;" jsshadow jsname="NuJwOd">
<div class="PI jh">
<div class="D5 fda">
<div class="F5">
<input type="text" class="Ij Sl" jsname="YPqjbf" autocomplete="off" tabindex="0" aria-label="Text input area where you can input a URL for the action." maxlength="1500" value="" autofocus="" data-initial-value="enter url here" badinput="false" dir="ltr" aria-invalid="false">
</div>
<div>.........</div>
I need to enter some input in this input field.For that i have used a locator
".Xz2Gac .WFUbU div:nth-child(6) .jxzYFc input"
But it says ElementNotVisibleError: element not interactable. But when i am giving the locators as
".Xz2Gac .WFUbU div:nth-child(7) .jxzYFc input"
then it is successfully entering the input data into that input field.I am not understanding why it so?because the input field is in 6th div of class 'WFUbU'.Am i right?.
if you exam the html sample that you provided, you can see that there is 6 childs only.
For the css matcher, possibly it ignore the condition to find the 7 child (which does not exist) and continue with matching next case.
With the following experiment:
.WFUbU .jxzYFc input
you will get the input
.WFUbU div:nth-child(6) .jxzYFc input
you again get again the input
Here is full explanation about nth:child
https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-child
Note: avoid creating such locators based on child of elements, due
dynamic changes of creation/updating a website.
If the unique jsname is always constat use something like:
CSS selector
input[jsname='YPqjbf']
Xpath selector
//input[#jsname='YPqjbf']
Try following two css locator manually in Chrome DevTool.
.Xz2Gac .WFUbU > div:nth-child(6) .jxzYFc input
.Xz2Gac .WFUbU div:nth-child(6) .jxzYFc input
If both locator find the same input, means there are one invisible input and one visible input. You should use .Xz2Gac .WFUbU div:nth-child(7) .jxzYFc input to find the visible input.
If they find different input, I think .Xz2Gac .WFUbU > div:nth-child(6) .jxzYFc input should find the visible input that's what you want.
> in css locator means to fine direct/first layer child.
(blankspace) in css locator means to find descendants which includes direct/first layer child
I am trying to select the value from a dropdown box using vba, the code block for the dropdown box is as follows
<input type="text" id="form_autocomplete_input-1542902425322" list="form_autocomplete_suggestions-1542902425322" placeholder="Search keyword or select filter" role="combobox" aria-expanded="false" autocomplete="off" autocorrect="off" autocapitalize="off" aria-autocomplete="list" aria-owns="form_autocomplete_suggestions-1542902425322 form_autocomplete_selection-1542902425322">
If the value of form_autocomplete_suggestions-1542902425322 was static I would use .Document.getElementById("form_autocomplete_suggestions-1542902425322").Value = "Role: Student" however this seems to be a randomly generated numerical value.
I have had a look and it seems I cannot simply add a wildcard in such as .Document.getElementById("form_autocomplete_suggestions-*").Value = "Role: Student"
And as its randomly generated and such a long number it cannot loop through an array of values. so I am unsure on how to solve this issue.
You can use css attribute equals value selector syntax with the ^ operator to say starts with a certain substring. You could also use * instead, which means contains.
[id^='form_autocomplete_input-']
VBA:
ie.document.querySelector("[id^='form_autocomplete_input-']")
You might also use:
[placeholder='Search keyword or select filter']
Which would be:
ie.document.querySelector("[placeholder='Search keyword or select filter']")
As you indicate this needs to be selected you may need:
ie.document.querySelector("[id^='form_autocomplete_input-']").Selected = True
Reference:
CSS attribute selectors
I have written a python selenium script that selects a state value from a drop down. The HTML for the drop down element is copied below:
<div class="hQSHyh4QFG0Xh0d-6pxTF" tabindex="0" style="height: 238px; display: none;">
<div class="SD_7vnwWhO0KG80czzPb3 option-0 al-option">AL</div>
<div class="SD_7vnwWhO0KG80czzPb3 option-1 ak-option">AK</div>
<div class="SD_7vnwWhO0KG80czzPb3 option-2 as-option">AS</div>
<div class="SD_7vnwWhO0KG80czzPb3 option-3 az-option">AZ</div>
<div class="SD_7vnwWhO0KG80czzPb3 option-4 ar-option">AR</div>
<div class="SD_7vnwWhO0KG80czzPb3 option-5 ca-option">CA</div>
<div class="SD_7vnwWhO0KG80czzPb3 option-59 um-option">UM</div>
</div>
Problem: the automation script locates the same state value ("CA") using a hard-coded xpath statement (See code snippet from script below). Instead, I would like to select the state value using a stored variable called "state".
state_selection = self.driver.find_element_by_xpath("/html/body/div[2]/div/div[2]/div/div/div[2]/div[1]/form/div/div[2]/div[2]/div[3]/div[2]/div/div[3]/div[6]")
state_selection.click()
Additional Notes: I have tried using other methods to locate the state value (see below) but, so far, I have only been successful using the hard-coded xpath above.
I also tried to locate the drop down element using the Selenium Select Method but I got messages telling me that "Select only works on <select> elements, not on 'div' "
driver.findElement(by.xpath("//select[#SD_7vnwWhO0KG80czzPb3='']/option[#value='CA']")).click()
Try to select required option by its text content:
state = "CA"
state_selection = self.driver.find_element_by_xpath("//div[.='%s']" % state)
state_selection.click()