Not able to get Xpath post applying starts with - selenium

My HTML Structure:
<form id="assignment-submission-540296" action="/assignments/submit-assignment" enctype="multipart/form-data" method="POST">
<input id="assignment_id" type="hidden" value="540296" name="assignment_id">
<input id="assignment_user_id" type="hidden" value="131639273" name="assignment_user_id">
<input type="hidden" value="/assignments/view/540296/131639273" name="returnUrl">
<div style="margin-right:230px;margin-top:-2px">
<input class="by-button fileuploadImage" type="button" value="Submit" data_id="" style="margin-left:155px">
My XPATH Generated with FIREPATH Is this:
.//*[#id='assignment-submission-540296']/input[4]
in the above x path 540296 is dynamic value hence I did something like this. Modified XPATH:
.//*[starts-with(#id,'assignment-submission']/input[4]
but after this not able to identify the element.

You are just missing a closing parenthesis:
.//*[starts-with(#id,'assignment-submission')]//input[4]
HERE ^
Also, you needed to use // after finding the form to search for desired input anywhere inside the form.
Besides, you can make the xpath a bit more specific by specifying a form tag and relying on input type:
.//form[starts-with(#id,'assignment-submission')]//input[#type="button" and #value="Submit"]

Related

How to write following sibling CSS/Xpath expression of username field?

HTML Code:
<form autocomplete="on" xpath="1">
<div class="IiD88i _351hSN">
<input class="_2IX_2- VJZDxU" autocomplete="off" type="text" value="">
<span class="_36T8XR"></span>
I can write this one : //input[#type='text']
But how to write it in "following sibling" CSS/xpath of the above code?
If you want to fetch input node based on its sibling, try:
//label[.='Enter Email/Mobile number']/preceding-sibling::input

In Selenium Webdriver using Java should I have to use element.click() before element.SendKeys for input text box?

driver.get("<url>")
WebElement name = driver.findElement(By.id(..));
name.click();
name.sendKeys("<input text>");
In the above code is it required to have name.click() first , or is it optional as the field is located and it should be able to send the input without having to click on it first
<input> tag
The <input> tag specifies an input field where the user can enter data. The <input> element can be displayed in different ways, depending on the type attribute. As an example:
<input type="text"> (default value)
<input type="button">
<input type="checkbox">
<input type="email">
<input type="file">
<input type="hidden">
<input type="number">
<input type="password">
<input type="radio">
<input type="submit">
<input type="url">
This usecase
As your usecase involves sending a character sequence presumably it should be of the following type:
<input type="text">
Generally you won't have to invoke click() before invoking sendKeys() but ideally to send a character sequence you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following locator strategies:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("birtviewer"))).sendKeys("Richard Bernard");
However, incase the element is a dynamic element having onfocus event attribute which fires the moment that the element gets focus as follows:
<input type="text" id="fname" onfocus="myFunction(this.id)">
you may require the additional step to click within the element.

VBA Macro in Excel - Using getElementById when the ID is dynamic

I am trying to fill out a form on a webpage using data from Excel and a VBA Macro.
IE.document.getElementById("elementName").Value = ThisWorkbook.Sheets("Table_1").Range("A2").Value
However, the website I am using doesn't have static IDs for the input IDs or the DIV tags. If I view the source on the webpage I will see this:
<div class="username" id="newid181619129-Username" value="">
<input class="search form-control" id="newid181619129-Username_input" type="text" />
</div>
And then if I refresh the webpage I get this.
<div class="username" id="newid521236985-Username" value="">
<input class="search form-control" id="newid521236985-Username_input" type="text" />
</div>
I have no control of the source webpage. The only solution I found for this was to "Enumerate all descendant <div>s of that tag, testing their ID property with Like." but there isn't a DIV tag I can find that uses a static ID.
Is there some way I can get getElementById to work with dynamic IDs?

Textbox and variables javascript and HTML

I want to set two JavaScript variables as the values of these textboxes.
Can anyone can help me?
<form name="myform1">
<input type="number" name="pop_size" value="3">
<input type="button" value="Pop Size" id="population" onclick="setValue()">
</form>
<form name="myform2">
<input type="number" name="totalIterations" value="2">
<input type="button" value="Iterations" id="Iterations" onclick="setValue()">
</form>
You can use getElementsByName() to get a list of elements by their names in the form. Since your names are unique (which isn't necessary in the spec, but is a good idea for this exact reason), the array returned by that function should have exactly one element in it. Something like this:
var firstVariable = document.getElementsByName('pop_size')[0].value;
var secondVariable = document.getElementsByName('totalIterations')[0].value;
Or did you mean that you want to set the values to what's in a variable? That would be the reverse:
document.getElementsByName('pop_size')[0].value = firstVariable;
document.getElementsByName('totalIterations')[0].value = secondVariable;

Using attachFile and Submit

I have the following HTML:
<form action="upload" method="post" enctype="multipart/form-data" >
Select a file to upload:
<input type="file" name="file"/>
<input type="hidden" name="USER_NAME" id="USER_NAME" value="user_name"/>
<input type="submit" value="Submit"/>
</form>
I'm having a couple of problems.
Firstly, in the entry field (named "file") I want to set the name of the file to upload. I can do that just with WebElement.type(), but I get a warning about using attachFile. I'm happy to use that, but when I do so I get the error:
com.thoughtworks.selenium.SeleniumException: Element must be user-editable in order to clear it.
My second problem is that if I set the content of the entry field by type(), the submit button doesn't work. I can get the WebElement by css, but neither click() or submit() works. Nothing happens at all when I do either.
Thanks in advance for any help, and apologies for dumbness.