質問

I am testing a scenario for the site "https://www.freecrm.com/index.html"

login credentials [ john2013 / john2013 ]

Scenario :

1 open the site https://www.freecrm.com/index.html

2 login  with valid credentials

3 click on the "New Contacts" link

4 Add new contacts

using Selenium ide i am able to login and click on the "New Contact" link , but when i am trying to do the same thing using Webdriver [ java] i am not able to click the "New Contact" link

the code i have written is given below

driver.findElement(By.name("username")).clear()     
driver.findElement(By.name("username")).sendKeys("john2013");
driver.findElement(By.name("password")).clear();
driver.findElement(By.name("password")).sendKeys("john2013");
driver.findElement(By.cssSelector("input[type=\"image\"]")).click();

The code up to the above is working fine but clicking the "New Contact" link

driver.findElement(By.xpath("//div[@class='noprint']/span[@class='headertext']/a[3]/")).click();

is not working though the same xpath is working in IDE.

i have tried with expected condition option , sleep but nothing is working.

can any one help me in this regard.

役に立ちましたか?

解決

The problem is that your page uses a frameset. You should tell your webdriver which frame to use when it will search for your element. So try this:

driver.findElement(By.name("username")).clear()     
driver.findElement(By.name("username")).sendKeys("john2013");
driver.findElement(By.name("password")).clear();
driver.findElement(By.name("password")).sendKeys("john2013");
driver.findElement(By.cssSelector("input[type=\"image\"]")).click();

//switch the driver to use one of the frames on your page. Potentially wait for a bit till the page is loaded
driver.switchTo().frame("mainpanel");

driver.findElement(By.xpath("//*[text()='New Contact']")).click();

他のヒント

Please change the xpath of link to

xpath=//a[contains(text(),'<whatevertext>')]

Incase if the same text link is already present in some part o the page we can proceed with occurences. Example if it is repeating second time:

xpath=(//a[contains(text(),'<whatevertext>')])[2]

If Nothing is working the best way is through java script executor.

First get the webelement of the link.

Then use the following code" webelement=driver.findElement(by.xpath("=//a[contains(text(),'')] ")) (JavaScriptExecutor)driver.executescript("argument[0].click();",webelement)

Actually Speaking //div[@class='noprint']/span[@class='headertext']/a[3]/ is absolute XPath.

as it ends with a[3] the position will vary always so it is advisable to use relative XPath/CSS

Suggested CSS:css=.noprint > .headertext > a.classname Another Suggested CSS: css=.noprint > .headertext > a[attribute='value']

So we can use either of the above format.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top