我正在尝试验证成功登录后网页上是否存在按钮。我正在使用Webdriver和Python。该按钮位于iFrame中。 这是我的第一个使用页面对象模型框架的webdriver python程序。到目前为止,我认为还不错。
程序成功登录但无法找到iFrame。 我需要一些帮助。
我收到以下错误:
Error
Traceback (most recent call last):
File "C:\Users\riaz.ladhani\PycharmProjects\Selenium Webdriver\TestCore 01\LoginPage_TestCase.py", line 16, in test_login_valid_user
login_page.isAdministration_present()
File "C:\Users\riaz.ladhani\PycharmProjects\Selenium Webdriver\TestCore 01\page.py", line 44, in isAdministration_present
content_frame = self.driver.find_element(*MainPageLocators.contentFrame)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 664, in find_element
{'using': by, 'value': value})['value']
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 175, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 166, in check_response
raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: Unable to locate element: {"method":"id","selector":"testcore"}
我的完整代码剪断如下:
# This class is where all my locators will be defined
from selenium.webdriver.common.by import By
class MainPageLocators(object):
GO_BUTTON = (By.ID, 'submit')
usernameTxtBox = (By.ID, 'unid')
passwordTxtBox = (By.ID, 'pwid')
submitButton = (By.ID, 'button')
AdministrationButton = (By.CSS_SELECTOR, 'div.gwt-HTML.firepath-matching-node')
AdministrationButtonXpath = (By.XPATH, '/body/div[2]/div[2]/div/div[2]/div/div[2]/div/div[7]/div/div')
AdministrationButtonCSS = (By.CSS_SELECTOR, '/body/div[2]/div[2]/div/div[2]/div/div[2]/div/div[7]/div/div')
contentFrame = (By.ID, 'testcore')
from element import BasePageElement
from locators import MainPageLocators
# This class is the base page class
class BasePage(object):
def __init__(self, driver):
self.driver = driver
# This class is the LoginPage. All the methods for the login page are defined here
class LoginPage(BasePage):
def click_go_button(self):
element = self.driver.find_element(*MainPageLocators.GO_BUTTON)
element.click()
def userLogin_valid(self):
userName_textbox = self.driver.find_element(*MainPageLocators.usernameTxtBox)
userName_textbox.clear()
userName_textbox.send_keys("user1")
password_textbox = self.driver.find_element(*MainPageLocators.passwordTxtBox)
password_textbox.clear()
password_textbox.send_keys("Pass1")
submitButton = self.driver.find_element(*MainPageLocators.submitButton)
submitButton.click()
# Is the Administration button present on the dashboard page
def isAdministration_present(self):
content_frame = self.driver.find_element(*MainPageLocators.contentFrame)
self.driver.switch_to.frame(content_frame)
administrationButtonCSS = self.driver.find_element(*MainPageLocators.AdministrationButtonCSS)
# This class is the TestCase test class for the Login page where all the test cases is defined
import unittest
from selenium import webdriver
import page
class LoginPage_TestCase(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.get("http://mypc:8080/testcore01")
def test_login_valid_user(self):
login_page = page.LoginPage(self.driver)
login_page.userLogin_valid()
login_page.isAdministration_present()
当我在Firefox中检查元素时,HTML如下:
<body style="margin: 0px;">
<iframe id="__gwt_historyFrame" style="position: absolute; width: 0; height: 0; border: 0;" tabindex="-1" src="javascript:''"/>
<iframe id="testCore" src="javascript:''" style="position: absolute; width: 0px; height: 0px; border: medium none;" tabindex="-1"/>
<div style="position: absolute; z-index: -32767; top: -20cm; width: 10cm; height: 10cm; visibility: hidden;" aria-hidden="true"/>
<div class="gwt-TabLayoutPanelTab GEGQEWXCK" style="background-color: rgb(254, 255, 238);">
<div class="gwt-TabLayoutPanelTabInner">
<div class="gwt-HTML">Administration</div>
答案 0 :(得分:2)
您的选择器中有拼写错误。
contentFrame = (By.ID, 'testCore') # capital "C"
应该是
<iframe id="testCore"...
根据你的HTML:
public class MainActivity extends ActionBarActivity {
ImageView addView, searchView;
DetailsEmp detailsEmp = new DetailsEmp();
ListView listView;
ArrayList<Employee> listDetails=new ArrayList<Employee>();
DetailsAdapter detailsAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Employee emp = (Employee) getIntent().getParcelableExtra(detailsEmp.PAR_KEY);
listView = (ListView) findViewById(R.id.listView);
addView = (ImageView) findViewById(R.id.addImage);
addView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(getApplicationContext(), DetailsEmp.class);
startActivity(i);
}
});
if (emp != null) {
Employee emplyoee=new Employee();
{
emplyoee.setName(emp.getName());
listDetails.add(emplyoee);
emplyoee.setCmpny(emp.getCmpny());
listDetails.add(emplyoee);
emplyoee.setDisig(emp.getDisig());
listDetails.add(emplyoee);
emplyoee.setListAge(emp.getListAge());
listDetails.add(emplyoee);
emplyoee.setListGen(emp.getListGen());
listDetails.add(emplyoee);
emplyoee.setListExp(emp.getListExp());
listDetails.add(emplyoee);
Log.d("bundle Size is ", "Emp Name is " + emp.getName());
Log.d("bundle Size is ", "Emp Sex is " + emp.getListGen());
Log.d("list Size is ", " ArrayList<Emplyoee> " + listDetails.size());
for (int i = 0; i < listDetails.size(); i++) {
Log.d("list Size is ", " ArrayList<Emplyoee> "
+ listDetails.get(i).getName().toString());
}
}
detailsAdapter=new DetailsAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, listDetails);
listView.setAdapter(detailsAdapter);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
// second activity
public class DetailsAdapter extends ArrayAdapter<Employee> {
private Context context;
private ArrayList<Employee> listDetails;
public DetailsAdapter(Context context, int resource,
ArrayList<Employee> listDetails) {
super(context, resource, listDetails);
// TODO Auto-generated constructor stub
this.context = context;
this.listDetails = listDetails;
}
private class ViewHolder
{
TextView empName,empCmpny,empDisig,empAge,empExp ;
ImageView empIcon;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder=null;
Employee emplyoee=getItem(position);
LayoutInflater inflator=(LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if(convertView == null)
{
convertView =inflator.inflate(R.layout.activity_listview, parent, false);
holder=new ViewHolder();
holder.empName=(TextView)convertView.findViewById(R.id.listName);
holder.empCmpny=(TextView)convertView.findViewById(R.id.listCmpny);
holder.empDisig=(TextView)convertView.findViewById(R.id.listDesignation);
holder.empIcon=(ImageView)convertView.findViewById(R.id.female);
convertView.setTag(holder);
}
holder=(ViewHolder)convertView.getTag();
for (int j = 0; j < listDetails.size(); j++) {
holder.empName.setText(emplyoee.getName());
holder.empCmpny.setText(emplyoee.getCmpny());
holder.empDisig.setText(emplyoee.getDisig());
if (emplyoee.getListGen().toString() == "Male") {
holder.empIcon.setImageResource(R.drawable.client_male_dark);
}
}
return convertView;
}
}
// POJO class
public class Employee implements Parcelable {
String listName = null;
String listCmpny = null;
String listDisig = null;
String listExp = null;
String listAge = null;
String listGen = null;
public String getName() {
return listName;
}
public String getListExp() {
return listExp;
}
public void setListExp(String listExp) {
this.listExp = listExp;
}
public String getListAge() {
return listAge;
}
public void setListAge(String listAge) {
this.listAge = listAge;
}
public String getListGen() {
return listGen;
}
public void setListGen(String listGen) {
this.listGen = listGen;
}
public void setName(String listName) {
this.listName = listName;
}
public String getCmpny() {
return listCmpny;
}
public void setCmpny(String listCmpny) {
this.listCmpny = listCmpny;
}
public String getDisig() {
return listDisig;
}
public void setDisig(String listDisig) {
this.listDisig = listDisig;
}
public static final Parcelable.Creator CREATOR = new Creator() {
@Override
public Employee createFromParcel(Parcel source) {
Employee emp = new Employee();
emp.listName = source.readString();
emp.listCmpny = source.readString();
emp.listDisig = source.readString();
emp.listAge = source.readString();
emp.listGen = source.readString();
emp.listExp = source.readString();
return emp;
}
public Employee[] newArray(int size) {
return new Employee[size];
}
};
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
dest.writeString(listName);
dest.writeString(listCmpny);
dest.writeString(listDisig);
dest.writeString(listAge);
dest.writeString(listGen);
dest.writeString(listExp);
}
}
// DetailsEmp
public class DetailsEmp extends Activity {
public final static String PAR_KEY="key_par";
private EditText empName, empCmpny, empDisig, empAge, empExp;
private RadioGroup empGender;
private RadioButton empMale, empFemale;
Button save;
ArrayList<Employee> list = new ArrayList<Employee>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details_emp);
// getting the details from the xml
empName = (EditText) findViewById(R.id.editName);
empCmpny = (EditText) findViewById(R.id.editCmpny);
empDisig = (EditText) findViewById(R.id.editDisignation);
empAge = (EditText) findViewById(R.id.editAge);
empExp = (EditText) findViewById(R.id.editExp);
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
empGenderDetails();
}
});
}
protected void empGenderDetails() {
// TODO Auto-generated method stub
// empMale = (RadioButton) findViewById(R.id.male);
Employee emp = new Employee();
emp.setName(empName.getText().toString());
//list.add(emp);
emp.setCmpny(empCmpny.getText().toString());
//list.add(emp);
emp.setDisig(empDisig.getText().toString());
//list.add(emp);
emp.setListAge(empAge.getText().toString());
//list.add(emp);
emp.setListExp(empExp.getText().toString());
//list.add(emp);
empFemale = (RadioButton) findViewById(R.id.female);
empGender = (RadioGroup) findViewById(R.id.radioGroup);
int sel = empGender.getCheckedRadioButtonId();
empMale = (RadioButton) findViewById(sel);
emp.setListGen(empMale.getText().toString());
Log.d("Employee data ", " Emp " + emp.listName.toString()
+ empMale.getText().toString()
+ empExp.getText().toString());
Intent i = new Intent(getApplicationContext(),
MainActivity.class);
Bundle b=new Bundle();
b.putParcelable(PAR_KEY , emp);
i.putExtras(b);
//setResult(RESULT_OK, i);
startActivity(i);
finish();
//list.add(emp);
Log.d("Emplyooe Size ","Emplyoee " + emp.getName());
/*
}