如何用Selenium识别这个元素,并将element.click()与数字变量一起使用



我已经想出了这个解决方案的大部分内容,但我无法理解这个(我是这个BTW的新手(。

我有一个网站,在一个框架中有一个可扩展的导航树,我需要扩展其中的所有元素,然后提取结果文本。

以下是我正在使用的基本模式。当我点击一个元素(class="x-tree-elbow-img x-tree-erbow-plus x-tree-expander"(时,它将打开下面的下一个元素;数据记录索引";属性。

首先,第一个数据记录索引是";0";所有其他项目都在下面。它已经展开/打开了,所以我必须跳过这个,否则我会点击它并折叠所有内容,没有任何代码可以搜索。如果我可以从数据记录索引="0"开始;1〃;并点击它,然后等待代码生成并在";2〃;等等。我的树上升到数据记录索引=";1083";处于启动状态。从上到下完全扩展它可能会产生超过5万条记录,因此每次点击都要等待一段时间。一旦它被展开,我就可以捕获所有的文本。

我想我应该找到包含数据记录索引="的标签;1〃;然后在其中找到具有包含"的类的;膨胀机";然后等待代码生成并返回将find-to-data记录索引递增为"0"的循环;2〃;等等,直到我用完了要查找的数据记录索引。完成后,它将被完全展开,我可以开始另一个过程打印出所有文本。

我知道我在第一次查找时没有正确使用计数变量,因为它会抛出一个错误。我还假设,一旦我找到第一个部分,我对扩展器链接的搜索就会起作用,因为它会找到它到达的第一个并点击它

这是脚本的循环部分,下面是HTML的代表部分。

顺便说一句,感谢@Grismar让我知道硒的存在!

''

我不知道我需要多少个循环,所以我只选了一个大数字,我相信有更好的方法可以做到这一点——比如直到

N = 1000000
for counter in range(N):
counter = counter + 1

如果我将其硬编码为数据记录索引="0";2〃;它找到了正确的记录。我只需要把计数器值传递到那个位置

datarecord = driver.find_element_by_xpath("//*[contains(@data-recordindex,'+counter+')]")

这种查找和单击可以工作,但并不限制查找从datarecord所在的标记开始

link = datarecord.find_element_by_xpath("//img[contains(@class,'expander')]")
link.click()

我正在搜索的HTML代码示例

<tbody id="treeview-1131-body">

<tr role="row" id="treeview-1131-record-9770" data-boundview="treeview-1131" data-recordid="9770"
data-recordindex="0" class="x-grid-row x-grid-tree-node-expanded x-grid-data-row" tabindex="-1"> 

<td role="gridcell"
class="x-grid-cell x-grid-td x-grid-cell-treecolumn-1129 x-grid-cell-treecolumn x-grid-cell-first x-unselectable x-grid-cell-treecolumn"
id="ext-gen13417">
<div unselectable="on" class="x-grid-cell-inner x-grid-cell-inner-treecolumn"
style="text-align:left;"><img
src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="
class=" x-tree-elbow-img x-tree-elbow-end-plus x-tree-expander"><img
src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="
class=" x-tree-icon x-tree-icon-parent x-tree-node-noicon"><span
class="x-tree-node-text ">Text to extract 1</span>
</div>
</td>
<td role="gridcell"
class="x-grid-cell x-grid-td x-grid-cell-actioncolumn-1130 x-grid-cell-last x-unselectable  x-action-col-cell"
id="ext-gen13418">
<div unselectable="on" class="x-grid-cell-inner x-grid-cell-inner-action-col"
style="text-align:left;"><img role="button" alt=""
src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="
class="x-action-col-icon x-action-col-0   x-tree-search"
data-qtip="L:eccng.widget.tree_search_tooltip"></div>
</td>
</tr>   

<tr role="row" id="treeview-1131-record-1490622" data-boundview="treeview-1131" data-recordid="1490622"
data-recordindex="1" class="x-grid-row x-grid-tree-node-expanded x-grid-data-row" tabindex="-1">
<td role="gridcell"
class="x-grid-cell x-grid-td x-grid-cell-treecolumn-1129 x-grid-cell-treecolumn x-grid-cell-first x-unselectable x-grid-cell-treecolumn"
id="ext-gen15793">
<div unselectable="on" class="x-grid-cell-inner x-grid-cell-inner-treecolumn"
style="text-align:left;"><img
src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="
class=" x-tree-elbow-img x-tree-elbow-empty"><img
src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="
class=" x-tree-elbow-img x-tree-elbow-plus x-tree-expander"><img
src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="
class=" x-tree-icon x-tree-icon-parent "><span class="x-tree-node-text ">Text to extract 2</span></div>
</td>

<td role="gridcell"
class="x-grid-cell x-grid-td x-grid-cell-actioncolumn-1130 x-grid-cell-last x-unselectable  x-action-col-cell"
id="ext-gen15794">
<div unselectable="on" class="x-grid-cell-inner x-grid-cell-inner-action-col"
style="text-align:left;"><img role="button" alt=""
src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="
class="x-action-col-icon x-action-col-0   x-tree-search"
data-qtip="L:eccng.widget.tree_search_tooltip"></div>
</td>
</tr>
</tbody>

''

想明白了,它就像一个符咒!我应该先读一下说明书,而不是花了整个周末的时间反复试验。

# Import Selenium items into Python.  I didn't use all these
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
import time
# Locate and start Chrome Test browser
PATH = "c:Program Files (x86)chromedriver.exe"
driver = webdriver.Chrome(PATH)
# Open website
driver.get("https://your.website.here/index.cfm")
# Give me time to log in
time.sleep(110)
# Go to the correct frame on the page
driver.switch_to.frame("dashboard-frame")

# Start a loop, hopefully a million is long enough
for i in range(1,1000000):
# Find the first node in the nav tree
datarow = driver.find_element_by_xpath("//tr[@data-recordindex = '"+str(i)+"']")
# Count the number of links to see what level of the tree it's in and print it all out
hierarchy = driver.find_elements_by_xpath("//tr[@data-recordindex = '"+str(i)+"']//img[contains(@src,'data')]")
level = len(hierarchy) 
print("Level: " +str(level)+ " " +datarow.text)
# Look for the link to click into the next level of the nav tree.  Need the Try and Except b/c the last level has no links
try:

link = driver.find_element_by_xpath("//tr[@data-recordindex = '"+str(i)+"']//img[contains(@class,'x-tree-expander')]")
link.click()
# Wait an ungodly amount of time for the code to populate before moving on.
time.sleep(10)
# This prints out all the errors, which happens to be the lowest level of tree. Nice surprise.
except NoSuchElementException:
print("Point: ")

最新更新