Selenide - found and fill the element - selenide

I failed to allocate following element via selenide (i need find and fill it with text (ID) :
<tr>
<th class="col-md-3 basicPropertiesName">Id</th>
<td class="col-md-9">
<input class="form-control ng-pristine ng-valid ng- touched" ng-keypress="keypressHandler($event, 2)" ng-model="newInsight.id" placeholder="ID">
</td>
</tr>

Or maybe this?
$("input[ng-model='newInsight.id']").setValue(value);

Maybe you need something like this?
By(xpath("//input[#ng-model='newInsight.id']))

$(By.xPath("//tr[./th[text()='Id']]")).find("input").setValue("some text)
or without additional find()
$(By.xPath("//tr[./th[text()='Id']]//input")).setValue("some text)

You could use following options using css selectors also.
String str="th[class*='basicPropertiesName']";
SelenideElement element =$(str);
or
String str="th:contains('Id')";
SelenideElement element =$(str);

one important moment.. all from above would fail if your example is placed in iframe (i've faced with such issue last week).
required actions:
a) how to find out if <tr> is (or not) part of iframe >> locate your tr on page, right-click on it, and ensure that you have no View frame source
example of page Inspect with iframe
if No such - try examples from above...
if There is one - see below
b) how to switch into iframe
you have to findout frame name...
--- make r-click on object + Inspert
--- locate your <tr>
--- click on it into Inspect
--- in the bottom of Inspert you'd see whole tree till selected element
--- move to the top of tree and you'd see something like iframe#framenamehere
--- copy it
before seaching your element add line like:
WebDriverRunner.getWebDriver().switchTo().frame(here the value from
prev. steps without #);
and search for your <tr>

I suggest that you use the th text in your query, to better target the input element:
Selenide.$(Selectors.byXpath("//th[text()='Id']/following-sibling::td/input"));
Note: Better to assign a unique id to the input element (and to every element under test) to improve readability and maintenance of your code.

Related

Excel VBA to Deal With AJAX

I am practicing to use excel vba to download information from website: http://mops.twse.com.tw/mops/web/t05sr01_1
But I have no idea how to download the data behind click button, as the image shown: http://i.stack.imgur.com/KZHiZ.jpg
I excerpt its web code as below. Could anyone explain me how to code in excel vba to get its data?
Thank you very mush.
Web code:
<td style='text-align:left !important;' nowrap>鴻海</td>
<td style='text-align:left !important;'>105/01/05</td>
<td style='text-align:left !important;'>11:41:00</td>
<td style='text-align:left !important;'>說明媒體報導</td>
<td><input type='button' value='詳細資料' onclick="document.fm_t05sr01_1.SEQ_NO.value='1';document.fm_t05sr01_1.SPOKE_TIME.value='114100';document.fm_t05sr01_1.SPOKE_DATE.value='20160105';document.fm_t05sr01_1.COMPANY_NAME.value='?E??';document.fm_t05sr01_1.COMPANY_ID.value='2317';document.fm_t05sr01_1.skey.value='2317201601051';document.fm_t05sr01_1.hhc_co_name.value='?E??';ajax1(this.form,'table01');">
You haven't shown how you are getting the html.
You can use a CSS selector.
General for first input button
input[type=button]
This says element(s) with input tag having attribute type whole value is 'button'
You apply with the querySelector method, or querySelectorAll if more than one match and then use index for required element.
ie.document.querySelector("input[type=button]").Click
If in an HTMLDocument variable e.g. htmlDoc then
htmlDoc.querySelector("input[type=button]").Click

Selenium click() or isSelected() are not executed on RadioButton's input element

I have radio buttons that are located inside a table, such as:
<tr id="radiofield-1080-inputRow">
<td class="x-form-item-body" id="radiofield-1080-bodyEl" colspan="3">
<input type="button" id="radiofield-1080-inputEl" class="x-form-field" autocomplete="off">
<label id="radiofield-1080-boxLabelEl" class="x-form-cb-label">My Label</label>
</td>
</tr>
I do find the input element, by the following code:
xPath = String.format("//tr/td[contains(#id,'%s')][contains(label,'%s')]/label", xType, text);
webElement = webDriver.findElement(By.xpath(xPath));
but isSelected() or click() doesn't seem to work on it.
Do you have any suggestion?
Haven't used selenium in a while but from a quick google it looks like you should try the isChecked and check/uncheck methods.
Here's the Javadoc but for some reason can't get a decent link, obviously check on the Selenium object. If you're using a different version or if I misunderstood something sorry.
http://selenium.googlecode.com/git/docs/api/java/index.html
In your code snippet problem with locator.
Use any of the below below locators
By.cssSelector("input[id*='radiofield-']");
By.id("radiofield-1080-inputEl")
By.xpath("//tr/td[contains(#id,'radiofield-')]/input")
Try clicking on the "input" instead of the "label".
xpath of input:
"//input[#id='radiofield-1080-inputEl']"
If the input id is not always the same, you can try this, the location of input will be based off the label:
//label[text()='My Label']/preceding-sibling::input
Thanks for all your answers.
My finding concluded with the following:
ExtJS implement radiobutton and checkbox as button, therefore the selenium isSelected() is not functioning.
There is a need to implement isSelected(), as suggested at:
How to check if extjs checkbox is selected in selenium?
The click() does the work, as it is a button.
Thanks again, Michal

Selenium WebDriver and xpath: a more complicated selection

So let's say my structure looks like this at some point:
..........
<td>
[...]
<input value="abcabc">
[...]
</td>
[...]
<td></td>
[...]
<td>
<input id="booboobooboo01">
<div></div> <=======I want to click this!
</td>
.........
I need to click that div, but I need to be sure it's on the same line as the td containing the input with value="abcabc". I also know that the div I need to click (which doesn't have id or any other relevant attribute I can use) is in a td at the same level as the first td, right after the input with id CONTAINING "boo" (dynamically generated, I only know the root part of the id). td's contain nothing relevant I can use.
This is what I tried as far as xpath goes:
//input[#value='abcabc']/../td/input[contains(#id,'boo')]/following-sibling::div
//input[#value='abcabc']/..//td/input[contains(#id,'boo')]/following-sibling::div
None of them worked, of course (element cannot be found).
I want to know if there's a way of selecting that div and how.
EDIT: //input[#value='abcabc']/../../td/input[contains(#id,'boo')]/following-sibling::div is the correct way. This was suggested by the person with the accepted answer. Also note that he offered a slightly different way of doing it. See his answer for details.
Try
//input[#value='abcabc']/ancestor::tr[1]/td/input[contains(#id,'boo')]/following-sibling::div[1]
Note that //input[#value='abcabc']/.. only goes up to the parent <td>, that's why your's did not work.
Another XPath that may work, is a bit more simple:
//input[#id='booboobooboo01']/../div[1]

Selenium IDE and xpath - find text / row in table and select radio box

I've been using Selenium IDE and getting some good results. I've done a lot of reading about following-sibling and preceding-sibling but I can't locate the right radio button.
Essentially I want to find the row in a table with the word 'testing' and then click the radio button in the cell.
So far I can find the input button
//input[#type='radio']
and find the text testing
//a[contains(text(),'testing')]
I've been trying to use this in the ide
check | //input[#type='radio']/following-sibling::td[1]/a[contains(text(),'testing')]
but I get the error [error] locator not found: //input[#type='radio']/following-sibling::a[contains(text()[1],'testing')]
Any help to change this is really appreciated :)
Cheers
Damien
here's the bare basic table ...
<tbody id="list">
<tr>
<th>
<label class="radio">
<input class="presentation_radio" type="radio" value="1" name="presentation_radio">
</label>
</th>
<td>
testing
</td>
<td>testing</td>
<td>Joe Acme</td>
<td>Presentation</td>
<td>03 May 2012</td>
<td>5 (1)</td>
</tr>
</tbody>
The problem with your xpath is that td and input are not sibling (they don't have common parent) and even if you change your xpath to more correct version:
//input[#type='radio']/following::td[1]/a[contains(text(),'testing')]
it will find a that have preceding checkbox instead of checkbox itself. So correct xpath will be:
//a[contains(text(),'testing')]/preceding::input[#type='radio'][1]
or
//tr[descendant::a[contains(.,'testing')]]//input[#type='radio']
For xpath axis tutorial read this: http://msdn.microsoft.com/en-us/library/ms256456.aspx

Xpath query to find elements which contain a certain descendant

I'm using Html Agility Pack to run xpath queries on a web page. I want to find the rows in a table which contain a certain interesting element. In the example below, I want to fetch the second row.
<table name="important">
<tr>
<td>Stuff I'm NOT interested in</td>
</tr>
<tr>
<td>Stuff I'm interested in</td>
<td><interestingtag/></td>
<td>More stuff I'm interested in</td>
</tr>
<tr>
<td>Stuff I'm NOT interested in</td>
</tr>
<tr>
<td>Stuff I'm NOT interested in</td>
</tr>
</table>
I'm looking to do something like this:
//table[#name='important']/tr[has a descendant named interestingtag]
Except with valid xpath syntax. ;-)
I suppose I could just find the interesting element itself and then work my way up the parent chain from the node that's returned, but it seemed like there ought to be a way to do this in one step and I'm just being dense.
"has a descendant named interestintag" is spelled .//interestintag in XPath, so the expression you are looking for is:
//table[#name='important']/tr[.//interestingtag]
Actually, you need to look for a descendant, not a child:
//table[#name='important']/tr[descendant::interestingtag]
I know this isn't what the OP was asking, but if you wanted to find an element that had a descendant with a particular attribute, you could do something like this:
//table[#name='important']/tr[.//*[#attr='value']]
I know it is a late answer but why not going the other way around. Finding all <interestingtag/> tags and then select the parent <tr> tag.
//interestingtag/ancestor::tr

Resources