我正在尝试单击Selenium中的链接(Python 2.7)。这是HTML代码:
<div class="buttons">
<a href="https://twitter.com/login?redirect_after_login=%2Flogout" class="btn cancel">Cancel</a>
<button type="submit" class="btn primary-btn js-submit">Log out</button>
</div>
我正在尝试点击“退出”按钮。我试过了:
elem = driver.find_element_by_link_text("Log out").click()
但这似乎不起作用。
任何帮助表示赞赏! :)
答案 0 :(得分:1)
“按链接文字”定位器仅适用于链接 - contact_view.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue obs, Object oldSelection, Object newSelection) {
if (newSelection != null) {
TableView.TableViewSelectionModel selectionModel = contact_view.getSelectionModel();
ObservableList selectedCells = selectionModel.getSelectedCells();
tablePosition = (TablePosition) selectedCells.get(0);
row = tablePosition.getRow();
col = tablePosition.getColumn();
Object colname = tablePosition.getTableColumn().getId();
val = (String) notes.getCellData(row);
String tmp="";
tmp = String.valueOf(cntac_name.getCellData(row));
frozenCol.setText(tmp);
System.out.println("Hola mundo 563 "+" "+row +" "+col+" "+val+" "+colname+" "+tmp);
// frozenCol.setText((java.lang.String) val);
if(col == 21) {
notes.setCellFactory(TextFieldTableCell.<Contact>forTableColumn());
notes.setOnEditStart(new EventHandler<CellEditEvent<Contact, String>>() {
@Override
public void handle(CellEditEvent<Contact, String> t) {
((Contact) t.getTableView().getItems().get(t.getTablePosition().getRow())).setNotes(t.getNewValue());
String cscode =t.getTableView().getItems().get(t.getTablePosition().getRow()).cstmr_code;
String newValue =t.getNewValue();
// notes.setText((java.lang.String) newValue);
System.out.println("Hola mundo 615 "+t.getNewValue()+" "+t.getTableView().getItems().get(t.getTablePosition().getRow()).cstmr_code);
String ok=String.valueOf(cssEditorFld.getText());
System.out.println("Hola mundo 617 "+" "+row +" "+col+" "+val+" "+cntac_name.getCellData(row)+" "+cssEditorFld.getText()+" "+ok);
// boolean r = CstmersDBConnection.UpdateSingleContact(cscode, cntac_code, colname, newValue);
//setCellFactory(SetEditorTxtArea());
Stage s=new Stage();
Object p =cntac_name.getCellData(row);
s.initModality(Modality.APPLICATION_MODAL);
s.initStyle(StageStyle.DECORATED);
s.setResizable(true);
s.setTitle(String.valueOf(p));
Group root = new Group();
GridPane gridpane = new GridPane();
gridpane.setPadding(new Insets(1));
gridpane.setHgap(1);
gridpane.setVgap(1);
cssEditorFld.setText(String.valueOf(val));
cssEditorFld.setPrefRowCount(20);
cssEditorFld.setPrefColumnCount(150);
cssEditorFld.setWrapText(true);
cssEditorFld.setPrefWidth(300);
GridPane.setHalignment(cssEditorFld, HPos.CENTER);
gridpane.add(cssEditorFld, 0, 1);
HBox hbox = addHBox();
gridpane.add(hbox, 0, 2);
root.getChildren().add(gridpane);
s.setScene(new Scene(root));
s.show();
buttonYs.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
System.out.println(" Clicked Yes");
String ok=String.valueOf(cssEditorFld.getText());
n= true;
s.hide();
((Contact) t.getTableView().getItems().get(t.getTablePosition().getRow())).setNotes(cssEditorFld.getText());
System.out.println("Hola mundo 627 "+" "+row +" "+col+" "+cssEditorFld.getText()+" "+((Contact) t.getTableView().getItems().get(t.getTablePosition().getRow())).notes);
}
});
System.out.println("Hola mundo 631 "+" "+row +" "+col+" "+val+" "+p+" "+cntac_name.getCellData(row)+" "+cssEditorFld.getText());
buttonNo.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
System.out.println(" Clicked No");
n = false;
s.hide();
}
});
}
});
元素。在这里,您可以使用XPath检查按钮的文本:
a
作为旁注,在此代码中:
driver.find_element_by_xpath("//button[. = 'Log out']").click()
将elem = driver.find_element_by_link_text("Log out").click()
的结果分配给变量没有多大意义 - 它总是click()
。