Selenium Python-如何引发异常并继续循环?

时间:2020-10-27 10:24:59

标签: python selenium pytest

如果发现任何不匹配,我想消除异常错误,但循环也应继续。 如果存在任何不匹配/异常错误,则整个案例都将失败。 你们可以检查下面的代码并在这里帮助我吗?

import 'package:flutter/material.dart';
import 'package:myapp/color_utils.dart';
class SeedConfirmPage extends StatefulWidget {
  SeedConfirmPage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _SeedConfirmPageState createState() => _SeedConfirmPageState();
}

class _SeedConfirmPageState extends State<SeedConfirmPage> {
  List<bool> clicked = [
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false,
    false
  ];
@override
  Widget build(BuildContext context) {
    final String seed = ModalRoute.of(context).settings.arguments;
    List<String> seedArray = seed.split(" ")..shuffle();
    final seedChip = <Widget>[];
    List<String> seedText = [];
    for (var i = 0; i < seedArray.length; i++) {
      seedChip.add(StatefulBuilder(builder: (BuildContext context, setState) {
        return MaterialButton(
          color: clicked[i] ? colorBlack : Colors.white,
          textColor: clicked[i] ? Colors.white : colorBlack,
          minWidth: MediaQuery.of(context).size.width,
          height: 32,
          child: Text(
            seedArray[i],
            style: TextStyle(fontSize: 12),
          ),
          onPressed: () {
            setState(() {
              clicked[i] = !clicked[i];
              if (clicked[i]) {
                seedText.add(seedArray[i]);
              } else {
                seedText.remove(seedArray[i]);
              }
            });
          },
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(4),
          ),
        );
      }));
    }
    return Scaffold(
      appBar: AppBar(
        backgroundColor: splashBG,
        leading: BackButton(color: Colors.white),
        title: Text("Back"),
      ),
      backgroundColor: splashBG,
      body: Container(
        padding: EdgeInsets.only(
          left: 16,
          right: 16,
        ),
        child: Column(
          children: <Widget>[
            Flexible(
              child: Container(
                width: MediaQuery.of(context).size.width / 2,
                child: Image(
                  image: AssetImage('assets/images/logo_1.png'),
                ),
              ),
            ),
            Container(
              height: 120,
              padding: EdgeInsets.all(32.0),
              margin: EdgeInsets.only(top: 28, bottom: 28),
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(8), color: colorBlack),
              child: Center(
                child: Text(
                  seedText.toString(),
                  style: TextStyle(color: Colors.white),
                ),
              ),
            ),
            Container(
              width: MediaQuery.of(context).size.width,
              height: 120,
              child: GridView.count(
                crossAxisCount: 4,
                crossAxisSpacing: 8,
                mainAxisSpacing: 8,
                childAspectRatio: MediaQuery.of(context).size.width /
                    (MediaQuery.of(context).size.height / 5),
                children: seedChip,
              ),
            ),
          ],
        ),
      ),
    );
  }
}

1 个答案:

答案 0 :(得分:0)

有一个变量url_mismatch,最初是False。而不是在URL不匹配时立即引发异常,只需将此变量设置为True。然后,当循环结束时,检查此变量的值并在变量为True时引发异常。

但是,不清楚您的try块是如何导致异常的。您可能是故意的(不需要try块):

if Redr_page == Newpage:
    print("Correct url")
else:
    raise Exception("Url mismatch")

现在,我保留那部分代码不变:

url_mismatch = False
while col_count<Total_entries:
    Webpage=old_urls[col_count]          //fetching data from 1st cell in the excel
    Newpage=new_urls[col_count]          //fetching data from 1st cell in the excel
    driver.get(Webpage)
    print("The old page url is: "+Webpage)
    page_title=driver.title
    print(page_title)
    Redr_page=driver.current_url
    print("The new url is: "+Redr_page)
    print("New_url from sheet:"+Newpage)

    try:
        if Redr_page==Newpage:
            print("Correct url")
    except:
        print('Mismatch url')
        url_mismatch = True # show we have had a mismtach

    col_count+=1
# now check for a mismatch and raise an exception if there has been one:
if url_mismatch:
    raise Exception("Url mismatch")