Swirch回到iframe selenium

时间:2017-05-16 09:42:20

标签: java selenium selenium-webdriver

我遇到了问题,我不知道自己哪里出错:

   Thread.sleep(10000);
    String mainWindow = driver.getWindowHandle();

    while(true) {
        try {
            wc.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@class='iff-campaign-container']/a")));
            driver.findElement(By.xpath("//*[@class='iff-campaign-container']/a")).click();
//Here on click , it open new window in new browser.
            for (String winHandle : driver.getWindowHandles()) {
                driver.switchTo().window(winHandle); 
            }
            driver.close();
            driver.switchTo().window(mainWindow);
            driver.switchTo().frame(0);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            break;
        }
    }

第一次循环运行后,我的等待条件失败。 我这样做,xpath在iframe中,并使用iframe,因为每次点击后都会添加新的webelement。

1 个答案:

答案 0 :(得分:1)

driver.switchTo().frame(0);行中,您要将驱动程序的TargetLocator更改为iframe,而xpath没有指定的iframe,因此等待条件失败

如果您在driver.switchTo().frame(0); // do stuff on the iframe driver.switchTo().defaultContent(); 内没有做任何事情,请将其删除。如果你在结束时使用它切换回来

export default class SettingsDialog extends React.Component {
  constructor(props) {
    super(props)

    this.state = {
      open: false,
      submittable: false,
      isSilent: false,
      forwardNumber: '',
      errorForwardNumber: ''
    }

    this.handleOpen = this.handleOpen.bind(this)
    this.handleClose = this.handleClose.bind(this)
    this.handleForwardNumberChange = this.handleForwardNumberChange.bind(this)
    this.handleIsSilentToggle = this.handleIsSilentToggle.bind(this)
  }

  //Validation Ommited

  handleForwardNumberChange(event, value) {
    const validation = this.validateNumber(value)
    this.setState({
      forwardNumber: value,
      errorForwardNumber: validation.error,
      submittable: validation.valid
    })
  }


  //Ommited

  render() {
    const actions = [
      <FlatButton label="Cancel" primary={true} onTouchTap={this.handleClose} />,
      <FlatButton
        label="Submit"
        primary={true}
        disabled={!this.state.submittable}
        onTouchTap={this.handleClose}
      />
    ]

    return (
      <div>
        <RaisedButton label="Settings" onTouchTap={this.handleOpen} />
        <Dialog
          title="Settings dialog"
          actions={actions}
          modal={true}
          open={this.state.open}
          onRequestClose={this.handleClose}
        >
          <TextField
            hintText="Enter a forwarding number"
            errorText={this.state.errorForwardNumber}
            fullWidth={true}
            floatingLabelText="Forwarding number"
            floatingLabelStyle={{ color: '#522560' }}
            defaultValue={this.state.forwardNumber}
            onChange={this.handleForwardNumberChange}
          />
          <br />
          <Toggle
            label="Silent"
            onToggle={this.handleIsSilentToggle}
            style={{
              toggle: {
                marginBottom: 16
              }
            }}
          />
        </Dialog>
      </div>
    )
  }
}