Can anyone help how to click the below link using selenium
<a class=”btn btn-primary btn-large” href="target-URL">Submit</a>
I tried using below options
linkText
partialLinkText
CssSelector
contains - logic which checks the URL text
You can try:
driver.findElement(By.xpath("//a[contains#class,'btn '] and contains(#class, 'btn-large') and contains(text(), 'Submit')")).click()
Give full xpath
driver.findElement(By.xpath("html/body/a").click();
Can try with tag name
driver.findElement(By.tagName("a").click();
Try with below xpath:
driver.findElement(By.xpath("//a[#href='target-URL']").click();
In theory, this is just:
driver.findElment(By.linkText("Submit")).click();
But, I'm pretty sure you've already tried that. Check if the element is in iframe/frame. If yes, you need to switch to it and only then find the link element:
driver.switchTo().frame("frame_name_or_id");
To switch back in the main context, use defaultContent():
driver.switchTo().defaultContent();
Related
I have the following element:
<div class="PickList visible"
widgetid="Palette" id="Palette">
<span class="stuff">
<span class="stuff"><span class="lbl">A-B</span><span class="no">1111</span>
</span>
<span class="stuffSelect"><span class="lblSelect">C</span><span
class="plu">2222</span></span>
The xpath that I am using is:
Driver.driver.findElement(By.xpath("//*[#id="Palette"]//span//span[2]//span[contains(text(),'C')]"));
It's still not able to pickup the letter 'C'.
Any suggestions appreciated. Thanks.
you can try below xpath to track from div.
driver.findElement("//*[#id='Palette']/span[2][#class='stuffSelect']/span[1][contains(text(), 'C')]");
xpath you are using is incorrect. I am providing you with the correct xpath or you can directly fetch it using the className as well.
Updated xpath as per the discussion:
WebElement selectedCharacter = driver.findElement(By.xpath("//div[#id='Palette']//span[#class='lblSelect']"));
selectedCharacter.getText();
By using className:
WebElement selectedCharacter = driver.findElement(By.className("lblSelect"));
selectedCharacter.getText();
Please try the below Xpath.It will print you 'C'
driver.findElement(By.xpath("(//div[#id='Palette']//span[1]//span[2]/span)[1]")).getText()
Here is the xpath. Consider the class visible in your xpath or css. As this denotes that this div might be not visible sometimes. So always be sure to use the visible if you have it as part of the class.
CSS
div.PickList.visible span.lblSelect
xpath
//div[#class='PickList visible']//span[#class='lblSelect']
<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
Is it possible to use linkText locator in this code
I used driver.findElement(By.linkText("welcome")).click();
But it didn't work.
Please help....
<div class="back-to">
<a class="button blue" href="javascript:history.back()">welcome</a>
</div>
The linkText should work in this case. Or else try the below alternatives(and please provide sufficient implicit timeout to give selenium sufficent time to detect the element):
1. Using xpath, to click on the element 'a' with exact innerHTML/text as 'welcome':
driver.findElement(By.xpath("//a[.='welcome']")).click();
2- Using JavascriptExecutor to click on the element with exact innerHTML/text as 'welcome':
((JavascriptExecutor)driver).executeScript("arguments[0].click();", driver.findElement(By.xpath("//a[.='welcome']")));
3- Using partialLinkText to click on the link with partial text 'welcome'
driver.findElement(By.partialLinkText("welcome")).click();
This Should be enough:
driver.find_element_by_xpath('//a[#class="button blue"]').click();
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.
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')]