【问题标题】:Is there a way I can modify some lines in site html code so it marks the checkbox?有没有一种方法可以修改站点 html 代码中的某些行,以便它标记复选框?
【发布时间】:2022-11-25 03:50:14
【问题描述】:

因此,我正在尝试解析一个网站,以便它可以每两小时自动提高我的出价。 该站点的设计方式是您必须用复选框标记要筹集的资金。 不知何故在 html 代码中,复选框没有值,而是看起来像这样:

我必须通过使用手动单击它

wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "idk what to write so it checks it"))).click()

但是我真的不知道如何找到它以便可以单击它。

<label>
  <input type="checkbox" value="613" checked=""> 
  # value - lot id, checked - means the checkbox is marked
<label>
  # and non-checked checkbox code looks like this:

<label>
  <input type="checkbox" value="613">
<label>

【问题讨论】:

    标签: python html selenium parsing beautifulsoup


    【解决方案1】:

    你不能在这里使用By.CLASS_NAME,因为它没有class。 你能够利用:


    chbVal = '613' # in case you need be able to change this
    
    (By.CSS_SELECTOR, f'label > input[type="checkbox"][value="{chbVal}"][checked=""]') # for checked
    
    (By.CSS_SELECTOR, f'label > input[type="checkbox"][value="{chbVal}"]:not([checked])') # for unchecked
    

    chbVal = '613' # in case you need be able to change this
    
    (By.XPATH, f'//label/input[@type="checkbox"][@value="{chbVal}"][@checked=""]') # for checked
    
    (By.XPATH, f'//label/input[@type="checkbox"][@value="{chbVal}"][not(@checked="")]') # for unchecked
    

    笔记:这些仅基于您包含的 html sn-p - 您可能需要在路径/选择器中包含具有更好标识符的父元素。


    还,

    不知何故在html代码中复选框没有价值

    但是in your snippet它做有value...?无论如何,上面的示例包括value,但您不必包括它们;你甚至可以排除他们与 not(...) 如 checked 所示。 (顺便说一句,not(checked)/not(@checked) 应该完全排除具有 checked 属性的元素,无论值是什么。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-28
      • 2017-02-03
      • 1970-01-01
      • 2018-10-08
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多