Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用Pypetteer/Puppeter点击dinamcly生成的按钮?_Python_Web Scraping_Automation_Pyppeteer - Fatal编程技术网

Python 如何使用Pypetteer/Puppeter点击dinamcly生成的按钮?

Python 如何使用Pypetteer/Puppeter点击dinamcly生成的按钮?,python,web-scraping,automation,pyppeteer,Python,Web Scraping,Automation,Pyppeteer,我正在使用Python无头浏览器库Pyppeteer。它基本上和木偶演员(JS)一样。所以对木偶演员有效的解决方案也应该在这里起作用。我需要点击一个按钮。问题是此按钮是动态生成的,其id每次都会更改: 按钮 我可以从它名字的那一部分找到那个按钮,它没有改变single\u button,但是还有另外3个按钮,它们的ID开始single\u button 页面中的其他按钮: 取消新安装(2) 取消此安装 取消此安装 使此按钮独特的两个因素是其id、最后一个数字和标题 如果你能帮我按一下这个按

我正在使用Python无头浏览器库Pyppeteer。它基本上和木偶演员(JS)一样。所以对木偶演员有效的解决方案也应该在这里起作用。我需要点击一个按钮。问题是此按钮是动态生成的,其id每次都会更改:

按钮 我可以从它名字的那一部分找到那个按钮,它没有改变
single\u button
,但是还有另外3个按钮,它们的ID开始
single\u button

页面中的其他按钮:
取消新安装(2)
取消此安装
取消此安装
使此按钮独特的两个因素是其id、最后一个数字和标题 如果你能帮我按一下这个按钮的标题,我将不胜感激


谢谢你们

检索更完整的CSS选择器。在Firefox开发工具中,可以通过右键单击元素>复制>CSS选择器来实现这一点,简单的使用包含文本XPATH选择器:
//按钮[@type='submit'并包含(,'Upgrade')]

在找到该元素之前,使用一个简单的while循环,而不是使用waitForSelector。 记住创建一个超时以避免无限循环

from time import sleep, process_time

...

start = process_time()
btn = []
while len(btn) == 0:
    elapsed = process_time() - start
    if elapsed > 0.30:
        break  # timeout 30s
    btn = await page.xpath('//button[@type="submit" and contains(., "Entrar")]')
    sleep(1)
   async def configure(self):
   browser = await launch()
   page = await browser.newPage()
   await page.goto('mysite.example')
   await asyncio.gather(
   page.waitForSelector('button[title="Upgrade Moodle database now"]', timeout=60000),
   page.click('button[title="Upgrade Moodle database now"]')
   )
<button type="submit" class="btn btn-secondary" id="single_button5ea4a114318a95" title="">Cancel new installations (2)</button>
<button type="submit" class="btn btn-secondary" id="single_button5ea4a114318a93" title="">Cancel this installation</button>
<button type="submit" class="btn btn-secondary" id="single_button5ea4a114318a94" title="">Cancel this installation</button>
from time import sleep, process_time

...

start = process_time()
btn = []
while len(btn) == 0:
    elapsed = process_time() - start
    if elapsed > 0.30:
        break  # timeout 30s
    btn = await page.xpath('//button[@type="submit" and contains(., "Entrar")]')
    sleep(1)