Python Selenium 属性值
Python Selenium attribute value
我需要名称为“serial”的属性值,但我在 python 和 selenium 方面的技能有限,无法获得该值。我正在寻找“0000013”的输出。并请指导我如何在循环中捕获元素。非常感谢。
我尝试过的:
for data in soup.find_all(class_='CoreData'):
h = data.find('h2')
k = h.find('serial')
print(k)
它returns值是“None”而不是“Serial”的值
<h2> <!--For Verified item-->
<a class="clickable" style="cursor:pointer;" onmousedown="open_item_detail('0000013', '0', false)" id="View item Detail" serial="0000013">
Sample Item
</a>
<!--For unverified item-->
</h2>
要获取 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"])
输出:
0000013
要打印属性 serial
的值,即 0000013,您需要引入 for the visibility_of_element_located()
and you can use either of the following :
使用CSS_SELECTOR
:
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”的属性值,但我在 python 和 selenium 方面的技能有限,无法获得该值。我正在寻找“0000013”的输出。并请指导我如何在循环中捕获元素。非常感谢。
我尝试过的:
for data in soup.find_all(class_='CoreData'):
h = data.find('h2')
k = h.find('serial')
print(k)
它returns值是“None”而不是“Serial”的值
<h2> <!--For Verified item-->
<a class="clickable" style="cursor:pointer;" onmousedown="open_item_detail('0000013', '0', false)" id="View item Detail" serial="0000013">
Sample Item
</a>
<!--For unverified item-->
</h2>
要获取 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"])
输出:
0000013
要打印属性 serial
的值,即 0000013,您需要引入 visibility_of_element_located()
and you can use either of the following
使用
CSS_SELECTOR
: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