Best Python code snippet using playwright-python
disambredir.py
Source:disambredir.py
1#!/usr/bin/python2"""3User assisted updating redirect links on disambiguation pages.4Usage:5 python pwb.py disambredir [start]6If no starting name is provided, the bot starts at '!'.7"""8#9# (C) Pywikibot team, 2006-202010#11# Distributed under the terms of the MIT license.12#13import pywikibot14from pywikibot import textlib, pagegenerators15from pywikibot.bot import (MultipleSitesBot, InteractiveReplace,16 AutomaticTWSummaryBot)17class DisambiguationRedirectBot(MultipleSitesBot, AutomaticTWSummaryBot):18 """Change redirects from disambiguation pages."""19 summary_key = 'disambredir-msg'20 def _create_callback(self, old, new):21 replace_callback = InteractiveReplace(22 old, new, default='n')23 replace_callback.allow_replace = True24 replace_callback.allow_replace_label = True25 replace_callback.allow_replace_section = True26 replace_callback.allow_replace_all = True27 return replace_callback28 def treat_page(self):29 """Iterate over linked pages and replace redirects conditionally."""30 text = self.current_page.text31 for linked_page in self.current_page.linkedPages():32 try:33 target = linked_page.getRedirectTarget()34 except (pywikibot.Error, pywikibot.SectionError):35 continue36 # TODO: Work on all links at the same time (would mean that the37 # user doesn't get them ordered like in links but how they appear38 # in the page)39 text = textlib.replace_links(40 text, self._create_callback(linked_page, target),41 self.current_page.site)42 if text != self.current_page.get():43 self.put_current(text)44def main(*args):45 """46 Process command line arguments and invoke bot.47 If args is an empty list, sys.argv is used.48 @param args: command line arguments49 @type args: str50 """51 local_args = pywikibot.handle_args(args)52 start = local_args[0] if local_args else '!'53 mysite = pywikibot.Site()54 try:55 mysite.disambcategory()56 except pywikibot.Error as e:57 pywikibot.bot.suggest_help(exception=e)58 return59 generator = pagegenerators.CategorizedPageGenerator(60 mysite.disambcategory(), start=start, content=True, namespaces=[0])61 bot = DisambiguationRedirectBot(generator=generator)62 bot.run()63if __name__ == '__main__':...
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!