pyppeteer 如何执行js函数定位路径变化的标签

遇到一场景,每次请求页面标签路径会变化,导致定位不到。

selenium 可以根据标签的text值进行定位,driver.find_element_by_xpath('//button/span[contains(text(), "指定文本内容")]').click(),

pyppeteer 执行xpath的操作没有测试。

async def click_credentials(page):
    num = 0
    while True:
        try:
            num += 1
            await asyncio.sleep(3)
            click_credentials_js = """()=>{
                var links=document.querySelectorAll("a");
                var lens=links.length;
                var lists=["凭据","Credentials","credentials"];
                for (var i=0;i < lens;i++){
                var tc = links[i].textContent;
                if(lists.indexOf(tc.trim()) != -1){
                    console.log(tc.trim());
                    links[i].click();
                    return true;
                }}}"""
            clickcredentials = await page.evaluate(click_credentials_js)
            if not clickcredentials:
                await page.reload()
                continue
            await asyncio.sleep(3)
            break
        except Exception as e:
            print('clickcredentials:',e)
        if num > 15:
            return

 获取a标签的textContent值,再进行下一步操作

 2.点击下拉框

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div class="Rte"><form method="post" action="/contact#contact_form" id="contact_form" accept-charset="UTF-8" class="Form Form--spacingTight"><input type="hidden" name="form_type" value="contact" /><input type="hidden" name="utf8" value="✓" /><div class="Form__Group">
          <div class="Form__Item">
            <input type="text" class="Form__Input" name="contact[name]" aria-label="Your name" placeholder="Your name" required >
            <label class="Form__FloatingLabel">Your name</label>
          </div>

          <div class="Form__Item">
            <input type="email" class="Form__Input" name="contact[email]" aria-label="Your email" placeholder="Your email" required >
            <label class="Form__FloatingLabel">Your email</label>
          </div>
        </div><div class="Form__Item"><input type="text" class="Form__Input" name="contact[Your phone number (Required)]" aria-label="Your phone number (Required)" placeholder="Your phone number (Required)" required>
                <label class="Form__FloatingLabel">Your phone number (Required)</label></div><div class="Form__Item">
              <div class="Form__Select Select Select--primary">
                  <select name="contact[Subject (Required)]" title="Subject (Required)" required>
                  <option value="" disabled selected>Subject (Required)</option><option value="Help placing an order">Help placing an order</option><option value="About my order">About my order</option><option value="Returns">Returns</option><option value="Gift vouchers &amp; discount codes">Gift vouchers &amp; discount codes</option><option value="Other ...">Other ...</option></select>
              </div>
            </div><div class="Form__Item"><input type="text" class="Form__Input" name="contact[Your BEAUTE order number (If applicable)]" aria-label="Your BEAUTE order number (If applicable)" placeholder="Your BEAUTE order number (If applicable)" >
                <label class="Form__FloatingLabel">Your BEAUTE order number (If applicable)</label></div><div class="Form__Item">
          <textarea name="contact[body]" cols="30" rows="10" class="Form__Textarea" aria-label="Your message" placeholder="Your message" required></textarea>
          <label class="Form__FloatingLabel">Your message</label>
        </div>
        <button type="submit" class="Form__Submit Button Button--primary Button--full">Send message</button></form></div>
</body>
</html>
click_credentials_js = """()=>{var links=document.querySelectorAll("form select > option");
                links[links.length-1].selected=true;}"""
await self.page.evaluate(click_credentials_js)

form select 表示form与select中间的空格,中间可能间隔多级标签

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值