【发布时间】:2016-12-29 09:09:09
【问题描述】:
我正在尝试使用来自http://selenium-python.readthedocs.io/locating-elements.html#locating-elements-by-class-name 的查找元素;然而,它们似乎只工作了一半,而且通常在更简单的网站上工作。我想知道为什么会这样。例如,目前我正在尝试定位:
<a class="username" title="bruceleenation" href="/profile/u/3618527996"></a>
使用:
content = driver.find_element_by_class_name('username')
但我什么也没得到。 html来自
https://gyazo.com/b2a0d389da26bbd325baaa5f915d0569 或
<body>
<nav id="nav-sidebar" class="nav-main"></nav>
<main id="page-content" class="" style="margin-right: 17px; margin-bottom: 0px;">
<header class="header-logged"></header>
<section class="page-content-wrapper"></section>
<section class="media-slider" style="display: block;">
<div class="close-slider"></div>
<section id="slider" class="open" style="display: inline-block;">
<a class="go-back" data-media-id="1322612612609855850_3618527996" title="Back to all media" href="javascript:void(0);"></a>
<section class="media-viewer-wrapper viewer" data-count-comments="0" data-count-likes="1" data-url-delete="/aj/d" data-url-comment="/aj/c" data-url-unlike="/aj/ul" data-url-like="/aj/l" data-user-id="3618527996" data-media-id="1322612612609855850_3618527996">
<section class="mobile-user-info"></section>
<section class="desktop-wrapper">
<section class="user-image-wrapper">
<div class="image-like-click"></div>
<a class="user-image-shadow" href="javascript:void(0);">
<img class="user-image" alt="" src="https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.0…493235_n.jpg?ig_cache_key=MTMyMjYxMjYxMjYwOTg1NTg1MA%3D%3D.2"></img>
</a>
<section class="image-actions-wrapper dropdown-anchor"></section>
</section>
<section class="media-viewer-info ui-front">
<section class="user-info-wrapper text-translate-parent-wrapper ">
<a class="user-avatar-wrapper profile" title="bruceleenation" href="/profile/u/3618527996"></a>
<section class="user-info">
<a class="username" title="bruceleenation" href="/profile/u/3618527996">
bruceleenation
</a>
<p class="full-name"></p>
<div class="media-date-geo">
<span></span>
</div>
</section>
有什么建议吗?我也试过 Xpath。 ["//a[@class='username'"]
【问题讨论】:
-
您确定该元素在您寻找它的时候就存在吗?页面动态更改其内容是相当普遍的。
-
另外,您是否尝试过执行 time.sleep(N)?通常,如果页面很复杂,它们可能需要一些时间来加载,因此某些组件不会立即出现在页面上。
标签: python selenium xpath find element