我有一个看起来像这样的网站,我想使用firefox + selenium + python提取uid字段的内容。每页只有1个UID。
<div class= "parent" >
<div class="module profile" dcp="1" uid="90">
...
...
</div>
</div>
具体而言,请参见以下内容:
<div class= "parent" >
<div class="module profile" dcp="1" uid="[RETURN THIS]">
...
...
</div>
</div>
我已经尝试了几种硒技术,包括使用
browser.find_elements_by_name
browser.find_elements_by_xpath
browser.find_elements_by_class_name
browser.find_elements_by_css_selector
但是它们都不能够返回UID。我要么得到一个空集,要么只得到一个类(即整个模块类,而不是DIV中的属性)。
我看到有些人推荐get_attribute选择器,但是我没有将其应用于此用例。
任何指导将不胜感激。
答案 0 :(得分:-1)
要提取属性 uid 的值,即文本 90 ,您可以使用Locator Strategies之一:
import math
import pygame as pg
pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
BG_COLOR = pg.Color('gray12')
radius = 60 # Circle radius.
IMAGE = pg.Surface((120, 120), pg.SRCALPHA)
pg.draw.circle(IMAGE, (225, 0, 0), (radius, radius), radius)
# Use this rect to position the image.
rect = IMAGE.get_rect(center=(200, 200))
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
elif event.type == pg.MOUSEMOTION:
mouse_pos = event.pos # Or `pg.mouse.get_pos()`.
# Calculate the x and y distances between the mouse and the center.
dist_x = mouse_pos[0] - rect.centerx
dist_y = mouse_pos[1] - rect.centery
# Calculate the length of the hypotenuse. If it's less than the
# radius, the mouse collides with the circle.
if math.hypot(dist_x, dist_y) < radius:
print('collision')
screen.fill(BG_COLOR)
screen.blit(IMAGE, rect)
pg.display.flip()
clock.tick(60)
pg.quit()
:
css_selector
myText = browser.find_element_by_css_selector("div.parent>div.module.profile").get_attribute("uid")
:
xpath
然而,属性 uid 似乎是文本 90 是动态元素,因此您必须为元素引入 WebDriverWait 可见,您可以使用以下任一解决方案:
myText = browser.find_element_by_xpath("//div[@class='parent']/div[@class='module profile']").get_attribute("uid")
:
css_selector
myText = WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.parent>div.module.profile"))).get_attribute("uid")
:
xpath
注意:您必须添加以下导入:
myText = WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='parent']/div[@class='module profile']"))).get_attribute("uid")