C# & WebDriver: Xpath Statement to a Web Driver Statement - c#

I am attempting to click on a button using Selenium WebDriver using an Xpath Identifier.
The HTML for the button object is as follows:
<div class="button-wrap fr">
<a class="button blue-button submit"><span class="button-left"></span><span class="button-mid"><span class="text-button-ds">Email me the link<span>Email me the link</span></span></span><span class="button-right"></span></a>
</div>
I was able to get the Xpath for the button. It is as follows:
//*[#id="forget-password-form"]/div/div[3]/div[2]/a/span[2]
I would like to write a Web Driver statement that clicks the button using an Xpath statement.
However, what I have written is not working (See below)
submitButton = driver.FindElement(By.XPath("//div[#id='forget-password-form'][3]/[2]"));
submitButton.Click();
Thanks in Advance

As an FYI, the following lines located the button and clicked it:
submitButton = driver.FindElement(By.XPath("//span[#class='button-mid']"));
submitButton.Click();
Cheers

Related

Unable to click login dropdown via xpath

I am on trying to click the login dropdown, then the login button at this address using xpath,
chrome found the xpath, but selenium will not find the element using xpath.
http://www.worldstarhiphop.com/videos/video.php?v=wshh41eS9O96e7OKbAoA
Here is my code:
driver.FindElement(By.XPath("//header[#id='main-nav']/nav[#class='nav nav-primary']//a[#role='menuitem']")).Click();
Why wont it locate the element using the xpath from chrome?
When Selenium opens the url the Login dropdown is not within the Viewport . To click the login dropdown you have to scroll the login dropdown WebElement within the Viewport first then invoke Click() as follows :
var login_dropdown = driver.FindElement(By.XPath("//header[#id='main-nav']/nav[#class='nav nav-primary']//a[#role='menuitem']"));
((IJavaScriptExecutor)driver).executeScript("return arguments[0].scrollIntoView(true);", login_dropdown);
login_dropdown.Click();

click on a <a> tag with selenium c#

I am trying to click on a <a> tag but when I am trying to access it by ID i got an error that says Unable to locate element.
I am using selenium c# in unit test project. the code to make the click is the followining:
IWebdriver.FindElement(By.Id("BUTTON_OPEN_SAVE_btn0_acButton")).Click();
and <a> tag code is the following:
<a id="BUTTON_OPEN_SAVE_btn0_acButton" ct="B" title="New Analysis of Existing Data" st="" href="javascript:void(0);" class="urBtnStd" ocl="sapbi_page.sendCommandArray([['BI_COMMAND_TYPE','LOAD',0],['TARGET_DATA_PROVIDER_REF','DP_1',0],['OPEN_SAVE_DIALOG_LAYOUT','DATAPROVIDER_PERSISTENCY',0]],event);" onkeydown="ur_Button_keypress(event);" onclick="ur_Button_click(event);" style="width:0;width:0;">New Analysis</a>
Try this:
IWebdriver.FindElement(By.XPath("//a[#class='urBtnStd']")).Click();
To click on the button with text as New Analysis you can use the following line of code :
IWebdriver.FindElement(By.XPath("//a[#class='urBtnStd' and [contains(.,'New Analysis')]]")).Click();

Clicking a text link - Using Selenium

The link that I am trying to click is not being recognized at all. Developer tools in chrome shows me the link as in structure below:
<main
<div
<ifreame
#document
<html
<body
<form
<div
::before
<a href=wizard_view.do? sys_action=sysverb_wizard_ans&WIZARD:action=follow&wiz_referring_url=&wiz_collection_key=&wiz_collectionID=&wiz_collection=&wiz_collection_related_field=&wiz_view=&wiz_action=sysverb_new&sys_id=ea7d96126f92a100f3ae60526e3ee4e0&sysparm_query=active=true&sysparm_target=&sys_target=change_ddfdff>Standard (from Template)
</a>
::after
</div>
....
How do I identify and click this particular text link?I tried css selector,xpath etc but didn't work
anchors = dd.findElements(By.linkText(Standard (from Template)));
anchors2 = dd.findElements(By.cssSelector(a[href*='ea7d96126f92a100f3ae60526e3ee4e0'] )));
Also there are a 10 div sections with same class name each of which is a text link. I want to click on of them (see above)
When you try to search for elements inside an iframe, you have to switch to that frame first
driver.SwitchTo().Frame(driver.FindElement(By.Id("_the_frame_id")))
Switching to frame solved my issue . But in the next page also I have the same freme (id/name) and I am not able to identify an input text box even though I have already switched to the frame.
I am getting 'NoSuchElementException' when:
driver.switchTo().frame(driver.findElement(By.id("gsft_main")));
WebElement stdFromTemplate = driver.findElement(By.linkText("Standard (from Template)"));
stdFromTemplate.click();
WebElement stTemplate = driver.findElement(By.id("cced8819db987e08dee9f7b31d961989_text"));
This last line(which is a new page now after clicking on the link that i was mentioning in the initial question) line give me 'NoSuchElementFoundException'

C# selenium cant find input in form

I am trying to locate a button in a form... but it doesnt find it...
I have tryed using threading.sleep(5000); but it dont help.
I have tryed so many ways but nothing helped me it always says that it cant locate the button...
here is my code:
IWebElement startb2 = driver.FindElement(By.XPath("//form/*[#name='start']"));
here is the html code of the form:
<form method="post" action="php/PickFruits.php">
<input type="submit" name="start" value="התחל לקטוף" class="button_submit" style="margin-right:-97px;">
</form>
I don't want to use the value because it is in Hebrew... and I cant use it in the c# console... please help me guys.
Edit:
Now its find the location of the input but it doesnt click on it... code:
IWebElement startb = driver.FindElement(By.XPath("//*[#type='submit']"));
startb.Click();
To switch to an iframe, use the below code.
//To find the iframe
IWebElement Object = driver.FindElement(By.XPath("//*[#class='active_iframe']");
//To switch to and set focus to the iframe
driver.SwitchTo().Frame(Object);
And then perform click operation.
IWebElement startb = driver.FindElement(By.XPath("//*[#type='submit']"));
startb.Click();

Select an Image button using Selenium inside anchor tag

How can i select an Image Button which look's like the below code Using Selenium in C#.I have Used Xpath to Find the Element.
<a onclick="resetValues();UploadFile();" href="#">
<img alt="Upload Selected" src="images/buttons/button_template.gif">
</a>
Here's the Xpath Which i have tried.Driver.FindElement(By.XPath(string.Format("//img[#alt='Upload Selected']/"))).Click()
Try using any of the Xpath from below. It should work for you
(By.xpath("//a[#onclick='resetValues();UploadFile();']//img"))
or
(By.xpath("//img[#src ='images/buttons/button_template.gif']"))
or
(By.xpath("//img[#src ='images/buttons/button_template.gif' and #alt='Upload Selected']"))
(Disclaimer: I am not familiar with Selenium, only with XPath.)
XPath expressions that end with / are not valid. Simply remove the / at the end:
Driver.FindElement(By.XPath(string.Format("//img[#alt='Upload Selected']"))).Click()
Also, I'm not sure why string.Format() is in there, but then again, I'm not familiar with Selenium.

Categories