Click span element using selenium - selenium

I would like to know how can I click on this element using selenium:
<span data-val="BB9049_600"> 7.5 </span>
Since there is no class or id with the span element I cant approach it that way.
The xpath is:
//*[#id="buy-block"]/div[1]/div[5]/div[3]/form/div[2]/div[2]/div/div/div/div[2]/div/ul/li[2]/span

You can try to use text content of required element as
//span[normalize-space()="7.5"]
or value of data-val attribute:
//span[#data-val="BB9049_600"]

you can use css selector as below
driver.findelement(By.CssSelector("[data-val='BB9049_600']"))
also by looking at 'data-val' value it seems 600 in 'BB9049_600' is not a constant value if that is the case then you can use below to check if selector starts with value
driver.findelement(By.CssSelector("[data-val^='BB9049_']"))

Related

How can I get an element without id, name and class attributes in red box using By?

<div class="bInputTab">
<ul>
<li class="onNow">网银支付</li>
<li>账号支付</li>
</ul>
</div>
How can I get the element in the red box using By?
Many thanks!
Try following xpath,
//a[#onClick='On click Value']
You could use xpath, tagName, all depends on HTML structure, You can find parent element, and search downwards:
//li[#class='onNow']/following-sibling::li[1]/a
if its only link in DOM driver.findElement(By.tagName("a"));
Hope this helps,
This XPath should work :
//li[#class='onNow']/following-sibling::li[1]/a
The link text should also work as well
driver.FindElement(By.LinkText("账号支付"));
Actually I did not show the key structure of the HTML. It is because the element is not in the default frame. So I add the WDS.browser.switchTo().frame("frame_main") to the code, it works.
Thanks for all of your help.
The reference is The WebDriver Sampler: Your Top 10 Questions Answered
The element’s locator is invalid
2. The element belongs to another frame
The element is not available in DOM yet

Select checkbox using input tag and non standard attribute values using python selenium

I want to select a checkbox with the HTML code shown below using the attribute bayid:
<input type="checkbox" devid="bay" bayid="10" checked="">
I could get the XPath information - "//*[#id="svbSelectEnc1"]/table/tbody/tr[7]/td[3]/input", but I want to use the bayid for selecting as there are lot of checkboxes in the form of a table and only specific checkboxes have to be selected that are read from the config file.
You can achieve it by using CSS Selector or XPath as shown below.
By CSS Selector
driver.findElement(By.cssSelector("input[bayid='10']")).click();
By XPath
//input[#bayid='10']
Also I would suggest you to go through basic tutorial on how to find WebElement using CSS Selector and XPath
try the following XPath:
//input[#bayid='10']
CSS selector way to do this:
driver.findElement(By.cssSelector("yourTagName[attribute='attributeValue']")).click();
For your specific case:
driver.findElement(By.cssSelector("input[bayid='10']")).click();

Contains CSS Selector to fill a form with nightwatch / selenium

My Problem:
The Page I try to test with NightwatchJS Contains Some Input Fields that have the Same beginning, but a random number is added. I want to Fill the Textfield on the page. Only one with this name is present on the same time.
<input id="groupNamec7aed06a-67a1-4780-9cc3-5b985666adb9" class="d-styled-input" data-value-update="keyup" data-bind="value: name" title="">
Is the definition of the Field. groupName is every Time the same, but the number changes.
Is there a possibility to use CSS Selector in nightwatch instead of XPATH?
You can try this way :
input[id^="groupName"]
From MDN > Attribute selectors :
[attr^=value] : Represents an element with an attribute name of attr and whose first value is prefixed by "value".
Unfortunately CSS Selector does not provide such a way. You could use a different CSS Selector to match inputs with an id and get those as a list. Afterwards using getAttribute('id') you could do it manually, but this seems like unnecessary effort to me and I'd recommend just using Xpath.
Ofcourse you could try and get a different unique CSS Selector. If it's in a form you could locate the form and use :nth-child but if I remember correctly this has limited/no support in IE.
Edit Apparently IE9 and later does support :nth-child

how to locate element with selenium webdriver for below html

I have an issue clicking on the below HTML:
<div id="P7d2205a39cb24114b60b80b3c14cc45b_1_26iT0C0x0" style="word-wrap:break-word;white-space:pre-wrap;font-weight:500;" class="Ab73b430b430a49ebb0a0e8a49c8d71af3"><a tabindex="1" style="cursor:pointer;" onclick="var rp=$get('ctl00_ContentPlaceHolder1_ReportViewer1_ctl10_ReportControl');if(rp&&rp.control)rp.control.InvokeReportAction('Toggle','26iT0C0x0');return false;" onkeypress="if(event.keyCode == 13 || event.which == 13){var rp=$get('ctl00_ContentPlaceHolder1_ReportViewer1_ctl10_ReportControl');if(rp&&rp.control)rp.control.InvokeReportAction('Toggle','26iT0C0x0');}return false;"><img border="0" src="/Reserved.ReportViewerWebControl.axd?OpType=Resource&Version=10.0.30319.1&Name=Microsoft.ReportingServices.Rendering.HtmlRenderer.RendererResources.TogglePlus.gif" alt="+"></a> 2013</div>
I have used the below script to click anchor inside a div tag. For the above html code it is not fixed only end part of id example "26iT0C0x0" is fixed. The script that I have used is:
WebElement e1=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[ends-with(#id,'26iT0C0x0')]/a")));
e1.click();
You can use the 'contains' method within an xpath lookup:
driver.findElement(By.xpath("//div[contains(#id,'26iT0C0x0')]")
I would recommend you to consider CSS selector alternative as CSS working faster, than xpath.
So 'contains' in attribute in CSS stands for '*=', for example
if we want to find attribute by 'CSS' ending in this: <htmlTag A="blablaCSS" > we need do the following:
String CSSselector="htmlTag[A*=CSS]";
and you get this element searched.
So considering your example CSS selector be like:
String cssSearched="div[id*=26iT0C0x0] a";
also try to click not on link - a
but on parent div as well:
String cssSearched="div[id*=26iT0C0x0]";
driver.findElement(By.cssSelector(cssSearched));
hope this works for you.
As Mark Rwolands already mentioned: the xpath-Function 'ends-with()' isn't supported in Selenium 2.
Also, if you maybe consider to use chromeDriver in the future, I would recommend clicking the image, not the anchor, see:
https://sites.google.com/a/chromium.org/chromedriver/help/clicking-issues
edit:
Also your IDs are looking generated. I wouldn't count on them for a stable test-environment.

how to click on element using WebDriver

i need to click on dat element:
<span id="act_action9" class="" onclick="openDialog('export', event)">
text
</span>
i cant click on id,because it is dynamic. After a click i am getting window with some settings.
You should use xpath in order to click this element, so you could add an expression that unequivocally identify this element.
Could you please include more HTML code, because just with the code included is not enough to create a xpath expression.
I recommend this tutorial to start doing good xpath expressions:
http://www.w3schools.com/xpath/
See example for dynamic xpath
By.xpath("//span[#id [contains(.,'act_action')]]")
Great xpath cheatsheets: http://extract-web-data.com/5-best-xpath-cheat-sheets-and-quick-references/
You can also use xpath like following(assuming only digit is dynamic):
".//span[contains(#id, 'act_action')]"
As ID is dynamic, you can use xpath for text which is inside span.
driver.findElement(By.xpath("//span[text()='text']").click();
Or if part of ID remain common, then you can use
//span[contains(#id,'act')]