我经常收到很多邮件,其中包含一些文件名和它们的ID(e.g. FILENAME_VERSION_ID)
。基于这些ID,我必须登录门户并分别下载每个文件。因此,我用基于Selenium的Python编写了一个脚本,用于自动下载这些文件
我的程序的工作方式如下:
脚本在一个名为ID.txt
的txt中提取这些文件的ID
然后,我使用for循环读取ID文件的每一行,直到它结束
所以我现在想要的是根据完整文件名(文件名的ID)中的部分文本从ID.txt中查找元素
with open('ID.txt') as f:
for line in f:
driver.find_element_by_xpath("//*[contains(@id,'%s')]" % str(line))
pyautogui.press('enter')
driver.find_element_by_xpath("//*[text()='ro']").click()
driver.find_element_by_xpath("//*[contains(@id,'%s')]" % str(line)).click()
driver.find_element_by_xpath("//*[text()='export']").click()
if 'str' in line:
break
显然,selenium找不到这行代码的元素
driver.find_element_by_xpath("//*[contains(@id,'%s')]" % str(line))
我要单击的站点上的一个元素如下所示:
<div index="0" aria-busy="false" aria-checked="false" aria-disabled="false" data-head="true" aria-label="09251561001.09251561001.1.31873860875, folder" aria-selected="false" class="option grid-row" role="option" id=":DOMLT_ELISYS:export:09251561001.09251561001.1.31873860875">
<div class="name-data icon folder" id="id1027">
<div class="progressbar" role="progressbar" aria-hidden="true" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100">
<div class="fill" role="presentation" style="width: 0%;"></div>
<div class="fill" role="presentation" style="width: 0%;"></div>
</div>
<a class="name-text" href="#">
<span>09251561001.09251561001.1.31873860875</span>
</a>
</div>
<div id="cellId1028" class="date-data"></div>
<div id="cellId1029" class="size-data"></div>
</div>
在我的ID.txt
文件中,仅存储点之后的最后一个数字(在本例中为31873860875)
我尝试了很多可能性,但都不起作用。我得到以下错误:
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[contains(@id,'31873860875
')]"}
我在这里不做什么?是否有其他方法可以在站点上选择/单击此元素
WebDriverWait
在这种情况下应该对您有所帮助:我相信在程序等待点击
text()='ro'
之后,它将能够点击"//*[contains(@id,'%s')]"
。如果没有,您可以对以下botton_to_click
执行相同的XPaths
操作当元素未加载且您试图定位它时,经常会发生这种情况。您可以使用等待来解决此问题,以便正确加载DOM。 https://selenium-python.readthedocs.io/waits.html
以下其中一项将非常有用
你离得够近了。文本
31873860875
在<span>
标记中带有,是text
因此,要定位元素,您需要为
visibility_of_element_located()
诱导WebDriverWait,您可以使用以下Locator Strategies之一:使用
XPATH
:注意:您必须添加以下导入:
相关问题 更多 >
编程相关推荐