[英]Trying to get text from span with Selenium and Python
EDIT: I figured out the culprit. 编辑:我找出了罪魁祸首。 That extra 's' certainly didn't do me any favors but the real issue was that I needed to switch to a new frame before searching for my element.
多余的“肯定”并没有给我任何帮助,但真正的问题是,在搜索我的元素之前,我需要切换到新的框架。 Works like a charm now.
现在就像魅力一样。 Thanks for all the help!
感谢您的所有帮助!
I'm trying to pull the number of total results from various text searches in ServiceNow. 我试图从ServiceNow中的各种文本搜索中提取总结果数。 I don't have access to the API so I'm having to brute force it a bit.
我没有访问该API的权限,因此我不得不对其进行强制破解。 Right now I have a python script that's using selenium to run the search.
现在,我有一个使用硒运行搜索的python脚本。 My problem is that for the life of me I can't scrape the results.
我的问题是我一生都无法抓到结果。
Here's the snippet of my code that isn't working: 这是我的代码片段无法正常工作:
elm_result = web_driver.find_elements_by_name("ts_count_8c58a5aa0a0a0b07008047e8ef0fe07d")
print("Total results: ", elm_result.text)
And here's the html and screenshot of the page it's pulling from: 这是它从中提取的页面的html和屏幕截图 :
<span name="ts_count_8c58a5aa0a0a0b07008047e8ef0fe07d" id="ts_count_8c58a5aa0a0a0b07008047e8ef0fe07d">
(162 matches)
<em>
<span class="search_no_results">No matches for <a title="" class="noresultlink" href="ticket_list.do sysparm_query=123TEXTQUERY321%3DSAP%5Eactive%3Dtrue">Tickets</a>
<span>
</span>
</span>
</em>
</span>
When I run the script it seems to hang on this last little before eventually throwing out the following error: 当我运行脚本时,它似乎挂了最后一点,最终抛出了以下错误:
Traceback (most recent call last):
File "...", line 35, in <module> print("Total results: ", elm_result.text)
AttributeError: 'list' object has no attribute 'text'
I've tried attacking it from a bunch of different angles but nothing seems to work. 我曾尝试从多个角度对它进行攻击,但似乎无济于事。 My goal is to get the "162 results" text into a variable that I can then pass onto a csv.
我的目标是将“ 162个结果”文本转换为变量,然后将其传递给csv。
You need to use find_element_by_name
instead of find_elements_by_name
. 您需要使用
find_element_by_name
而不是find_elements_by_name
。 find_elements_by_name
returns a WebElement, which has an attribute text
, while find_elements_by_name
return a list of WebElement. find_elements_by_name
返回一个WebElement,该WebElement具有一个属性text
,而find_elements_by_name
返回一个WebElement列表。
Here is examples. 这是例子。
elm_result = web_driver.find_element_by_name("ts_count_8c58a5aa0a0a0b07008047e8ef0fe07d")
print("Total results: ", elm_result.text)
or you can specify an index of the list. 或者您可以指定列表的索引。
elm_result = web_driver.find_elements_by_name("ts_count_8c58a5aa0a0a0b07008047e8ef0fe07d")
print("Total results: ", elm_result[0].text)
EDIT 编辑
There is another problem which is about wait time for the login. 还有另一个问题是有关登录的等待时间。 You can add thread sleep or using Selenium wait for a specific element.
您可以添加线程睡眠或使用Selenium等待特定元素。
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
...
...
elm_result = WebDriverWait(web_driver, 10).until(EC.visibility_of_element_located((By.NAME, 'ts_count_8c58a5aa0a0a0b07008047e8ef0fe07d')))
print("Total results: ", elm_result.text)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.