I am trying to write selenium webDriver code for below HTML Code and it is throwing the error like:
Unable to find element
Can any help on this?
HTML Code :
<input class="_3uUUD5" type="checkbox" is="null" readonly="">
<div class="_1p7h2j" is="null"></div>
<div class="_1GEhLw" is="null">Gionee</div>
I am trying to select the check box for above mentioned HTML Code and please find my code below. I tried:
bothd.findElement(By.xpath("/html/body/div[1]/div/div[2]/div/div[2]/div[2]/div/div[4]/section/div[2]/div/div[1]/div[2]/div[72]/div/div/div/label/div[1]")).click();
or
d.findElement(By.className("_1p7h2j")).click();
If you want to locate checkbox element wrt Gionee text, try using below xpath :-
.//input[following-sibling::div[text()='Gionee'] and #type='checkbox']
Related
I have below snippet of html code which is created using reactjs and could you please help me out finding xpath for the same? When I try with chromepath it is throwing this error "This is a pseudo element and selectors can't be generated for pseudo element".
Snippet:
I am giving correct XPath. there is only one matching node too. but it still gives me issue.
I am using TestNG
My Code is :
driver.navigate().to("http://uk.support.tomtom.com/app/questions/extended_warranty");
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
driver.findElement(By.xpath("//input[#id='IDToken1']")).sendKeys("myemailaddress");
HTML is :
<input id="IDToken1" class="form-control" name="IDToken1" value="" data-validation="text" data-required="true" onkeydown="enableSubmit();" onchange="enableSubmit();" onkeyup="enableSubmit();" onmouseup="enableSubmit();" placeholder="example#tomtom.com" type="text"/>
I just logged into the website : http://uk.support.tomtom.com/app/questions/extended_warranty
and I found that the xpath you are providing seems to be incorrect. Use the xpath as shown below , I found that to be the xpath of the email address field. Give it a shot
//*[#id='rn_LoginFormRedirect_3_Username']
And my suggestion would be to use the add on's firebug and firepath for firefox much easier to locate elements using them.
You can use following code. It works:-
driver.navigate().to("http://uk.support.tomtom.com/app/questions /extended_warranty");
driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
driver.findElement(By.id("rn_LoginFormRedirect_3_Username")).sendKeys("myemailaddress");
I have been trying to click on a link in my application at work , but the control failed to click on the link whichever locator I used. I test all the locators with Selenium IDE and javascript before using them in the script and it all seem fine prior to running the test. I manage to identify and click on the link with this:
#FindBy(css ="/html/body/div[4]/div/div/div[2]/div[1]/a/strong") but I ran the test the second time but it failed to click on the link same.
See below the html code:
<div class="container container-outer">
<div class="row-fluid long-text-fitted content-container" id="page-content-container">
<div class="span12">
<div class="row-fluid container-alert">
</div>
<div class="left-side-spacer-layout right-side-spacer-layout" id="page-content">
<div class="button-spacer" id="breadcrumb-content">
<a href="/shopping/marketplace/landingPage">
<strong>< Back to Search Results</strong>
</a>
You should click on parameter. remove strong attribute.
/#FindBy(css ="/html/body/div[4]/div/div/div[2]/div[1]/a")
I would avoid using XPath in general and especially an XPath that starts at the HTML tag. That makes the test very fragile. I would suggest a CSS selector like the below.
driver.findElement(By.cssSelector("#breadcrumb-content > a")).click();
The CSS selector reads find an A tag that is a child (>) of an element with an ID (#) of breadcrumb-content.
Read more about CSS Selectors.
Using absolute Xpath is not a good idea. As you said it is working for the first time then possible your DOM strcture is changing after click it once
You can try with below xpath:
driver.findElement(By.xpath("//strong[contains(.,'< Back to Search Results')]")).click();
If still not working, you should try to click using JavascriptExecutor as it directly working on HTML DOM. Feel free to locate element in below code according to your convenience:
WebElement element=driver.findElement(By.xpath("//strong[contains(.,'< Back to Search Results')]"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);
<div class="list_details">
<p class="lnk_primary show_inline" rv-text="config.app.message.Label.abc.SmallScreen.58384" rv-on-click="current.eventGoToName">Add name</p>
</div>
I tried finding the link 'Add name' by using 'linktext' and 'partiallinktext' but it throws unable to locate element. I tried using classname but it also failed. Finally i used xpath to work.
My Code:
driver.findElement(By.linkText("Add name")).click();
driver.findElement(By.partiallinkText("name")).click();
driver.findElement(By.className("lnk_primary show_inline")).click();
Please let me know whether am i making any mistake?
Sample program to find link using linktext :
FirefoxDriver driver = new FirefoxDriver();
driver.get("https://www.google.co.in/?gfe_rd=cr&ei=2FqBVNuuJOzV8gea24GwDA&gws_rd=ssl#q=google");
driver.findElement(By.linkText("Google")).click();
please share your site URL so I can check.
By.linkText and By.partiallinkText are only looking for the
<a> tag. Since your link is in a <p> tag, it will not be found. Finding the element by xpath, like you have been able to do, is the better solution!
Based off experience and this question, By.className will not work for finding elements with more than one class. You could use xpath again or By.cssSelector instead to locate the element, such as:
By.cssSelector('.lnk_primary.show_inline')
I am trying to find Submit element. My HTML structure is as below.
<div>
<span class="combutton">Submit</span>
</div>
<div>
<span class="combutton">Cancel</span>
</div>
In browser using firebug I tried
$('div .combutton')[0].click()
which clicks on submit perfectly. But using selenium driver this element is not found. Please tell me how to do this using
driver.findElement(By.css("CSSSELECTORSTRING"))
What you did in Firebug shouldn't have any effect since it's clicking on a span and not the a inside it.
This should work, unless you omitted certain parts of your markup that would otherwise prevent it:
driver.findElement(By.cssSelector("div:first-child .combutton a")).click();
try that :
driver.findElement(By.xpath("//div span.combutton a[contains(.,'Submit')]")).click();
or
driver.findElement(By.xpath("//div span.combutton[0] a")).click();