[英]Python Selenium attribute value
I need the value for attribute with the name "serial" which I am unable to get with my limited skills in python & selenium.我需要名称为“serial”的属性值,我在 python 和 selenium 方面的技能有限,无法获得该值。 I am looking for the output of "0000013".我正在寻找“0000013”的输出。 and please guide me how to capture the element in a loop as well.并请指导我如何在循环中捕获元素。 many thanks.非常感谢。
What I have tried:我尝试过的:
for data in soup.find_all(class_='CoreData'):
h = data.find('h2')
k = h.find('serial')
print(k)
It returns value of "None" instead of the value of "Serial"它返回“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>
To get the value of serial
, try the following:要获取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"])
Output:输出:
0000013
To print the value of the attribute serial
ie 0000013 you need to induce WebDriverWait for the visibility_of_element_located()
and you can use either of the following Locator Strategies :要打印属性serial
的值,即0000013,您需要为visibility_of_element_located()
引入WebDriverWait ,您可以使用以下任一定位器策略:
Using CSS_SELECTOR
:使用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"))
Using XPATH
:使用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"))
Note : You have to add the following imports :注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.