使用黄瓜和selenium-webdriver但是,单击页面上的链接不起作用。
HTML代码:
<li id="menu-item-325" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-325">
<a href="mysite/contact/" class="nav-top-link">Contact</a>
</li>
测试代码:
const { defineSupportCode } = require('cucumber');
const { Builder, By } = require('selenium-webdriver');
const driver = new Builder()
.forBrowser('chrome')
.build();
defineSupportCode(function ({ Given, When, Then }) {
// Navigation to the url works
Given('I am on the site homepage', function () {
return driver.get(url);
});
When('When I click Contact', function() {
return driver.findElement(By.linkText('Contact')).click();
});
});
我也尝试过:
return driver.findElement(By.xpath("//a[@href='mysite/contact/']")).click();
答案 0 :(得分:0)
要点击带有测试的链接 Contact
,您可以使用以下代码行:
driver.findElement(By.xpath("//li[@class='menu-item menu-item-type-post_type menu-item-object-page' and starts-with(@id, 'menu-item-')]/a[@class='nav-top-link']")).click();
我们可以更加细化:
driver.findElement(By.xpath("//li[@class='menu-item menu-item-type-post_type menu-item-object-page' and starts-with(@id, 'menu-item-')]/a[@class='nav-top-link' and @href='mysite/contact/']")).click();
注意:
click()
方法返回无效
答案 1 :(得分:0)
有时在点击之前提供睡眠工作..尝试等待或使用Thread.sleep
正常点击不起作用,即使是睡眠,也请尝试
使用moveToElement执行操作,然后单击。
你也可以在最后的时候尝试使用javascript。
void transaction :: process()
{ int bflag=0;
if(type==0||type==1)
{ account b;
fstream file("Accounts.bof",ios::binary);
file.seekg(0,ios::beg);
while(!file.eof())
{ file.read((char*)&b, sizeof(b));
if(file.eof())
break;
if(b.get_acno()==payee.ac)
{ if(b.get_balance()>=amount)
{ b.minus_money(amount);
bflag=1;
file.seekp(-1*(sizeof(b)),ios::cur);
file.write((char*)&b, sizeof(b));
b.out(); //to output account's details
getch();
break;
cout<<"Money deducted";
getch();
}
}
file.write((char*)&b, sizeof(b));
}
if(bflag==0)
cout<<"\n\n\t\tTransaction unsuccessful!!!";
cout<<"\n\t\tCause : Insufficient Balance.";
file.close();
}
if(type==0||type==2)
{ account b;
fstream file("Accounts.bof",ios::binary);
while(!file.eof())
{ file.read((char*)&b, sizeof(b));
if(!file.eof())
break;
if(b.get_acno()==recieptent.ac)
{ b.add_money(amount);
cout<<"money added";
file.seekp(-1*(sizeof(b)),ios::cur);
file.write((char*)&b, sizeof(b));
break;
}
file.write((char*)&b, sizeof(b));
}
file.close();
}
if(bflag)
cout<<"\n\n\t\tTransaction successful.";
getch();
}
答案 2 :(得分:0)
简单使用xpaths
喜欢这个
//a[contains(text(),'Contact')]
代码:
driver.findElement(By.xpath("//a[contains(text(),'Contact')]")).click();
OR
//a[contains(text(),'Contact') and contains(@class,'nav-top-link')]
代码:
driver.findElement(By.xpath("//a[contains(text(),'Contact') and contains(@class,'nav-top-link')]
")).click();
同时尝试xpath
和javaScriptExecutor
WebElement elemenLink = driver.findElement(By.xpath("//a[contains(text(),'Contact') and contains(@class,'nav-top-link')]"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", elemenLink );