Python Selenium属性值

Python Selenium属性值,python,selenium,xpath,beautifulsoup,webdriverwait,Python,Selenium,Xpath,Beautifulsoup,Webdriverwait,我需要名为“serial”的属性的值,这是我在python和selenium方面的有限技能所无法获得的。我正在寻找“0000013”的产量。请指导我如何捕获循环中的元素。非常感谢 我所尝试的: for data in soup.find_all(class_='CoreData'): h = data.find('h2') k = h.find('serial') print(k) 它返回的值为“None”,而不是“Serial” 样品 要获取序列号的值,请尝试以下操

我需要名为“serial”的属性的值,这是我在python和selenium方面的有限技能所无法获得的。我正在寻找“0000013”的产量。请指导我如何捕获循环中的元素。非常感谢

我所尝试的:

for data in soup.find_all(class_='CoreData'):
    h = data.find('h2')
    k = h.find('serial')
    print(k)
它返回的值为“None”,而不是“Serial”


样品

要获取序列号的值,请尝试以下操作:

from bs4 import BeautifulSoup

html = """
<h2>
 <!--For Verified item-->
 <a class="clickable" id="View item Detail" onmousedown="open_item_detail('0000013', '0', false)" serial="0000013" style="cursor:pointer;">
  Sample Item
 </a>
 <!--For unverified item-->
</h2>"""

soup = BeautifulSoup(html, "html.parser")

for data in soup.find_all("a", class_="clickable"):
    print(data["serial"])

要获取序列号的值,请尝试以下操作:

from bs4 import BeautifulSoup

html = """
<h2>
 <!--For Verified item-->
 <a class="clickable" id="View item Detail" onmousedown="open_item_detail('0000013', '0', false)" serial="0000013" style="cursor:pointer;">
  Sample Item
 </a>
 <!--For unverified item-->
</h2>"""

soup = BeautifulSoup(html, "html.parser")

for data in soup.find_all("a", class_="clickable"):
    print(data["serial"])

要打印属性
serial
的值,即0000013,需要对位于()的元素的
可见性进行归纳,并且可以使用以下任一项:

  • 使用
    CSS\u选择器

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "h2 a.clickable[onmousedown^='open_item_detail']"))).get_attribute("data-clipboard-text"))
    
  • 使用
    XPATH

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h2//a[@class='clickable' and starts-with(@onmousedown, 'open_item_detail')]"))).get_attribute("serial"))
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

要打印属性
serial
的值,即0000013,您需要对位于()的元素的
可见性进行归纳,并且您可以使用以下任一项:

  • 使用
    CSS\u选择器

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "h2 a.clickable[onmousedown^='open_item_detail']"))).get_attribute("data-clipboard-text"))
    
  • 使用
    XPATH

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h2//a[@class='clickable' and starts-with(@onmousedown, 'open_item_detail')]"))).get_attribute("serial"))
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

我没有看到类
CoreData
。请更新更多的HTML和您尝试的完整代码。我看不到类
CoreData
。请更新更多的HTML和您尝试的完整代码