我的兄弟要求我的跛脚蟒蛇技能,以便建立一个监控来自纳斯达克股票数据的程序,例如 - the apple stock
我想通过XPATH
获取今天的高/低价值(目前为141.3893美元/ 139.76美元),因此,我使用此代码:
el = WebDriverWait(driver1, 10).until(
EC.visibility_of_element_located((By.XPATH, "//tr[contains(lower-case(text()), 'Today's High /Low')]")))
我的逻辑 - 找到包含“今天的高/低”文本的tr
标签,然后我要打印第二个td
的文本,我认为这种模式是在每个库存上这个网站。
然而,它引发了TimeoutException
错误:
Traceback (most recent call last):
File "C:/Users/Guy Balas/PycharmProjects/stockmarket/main.py", line 49, in <module>
main('http://www.nasdaq.com/symbol/aapl')
File "C:/Users/Guy Balas/PycharmProjects/stockmarket/main.py", line 42, in main
s = get_data()
File "C:/Users/Guy Balas/PycharmProjects/stockmarket/main.py", line 33, in get_data
EC.visibility_of_element_located((By.XPATH, "//td[contains(lower-case(text()), 'Today's High /Low')]"))).text
File "C:\Users\Guy Balas\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
任何其他方法可以实现这个/我犯的任何错误?
感谢。
答案 0 :(得分:1)
您可以按照以下方式执行此操作:
el = WebDriverWait(driver1, 10).until(
EC.visibility_of_element_located((By.LINK_TEXT, "Today's High /Low")))
print(el.find_element_by_xpath('./following::td').text)
输出$ 141.3893 / $ 139.76
答案 1 :(得分:1)
我知道你接受了答案,但有一种更简单的方法可以做到这一点。如果你查看页面的HTML,你会看到这个
<tr>
<td>
<a class="tt show-link" id="todays_high_low" onmouseover="showDelayedToolTip('todays_high_low')" onmouseout="hideToolTip('todays_high_low')" href="javascript:void(0)">
Today's High /Low
<span class="tooltipLG">...snip...</span>
</a>
</td>
<td align="right" nowrap="">
<label id="Label3">$ 141.3893</label> /
<label id="Label1">$ 139.76</label>
</td>
</tr>
从HTML中,您可以看到每个都有一个ID,“Label3”(高)和“Label1”(低)。有了这些信息,您可以使用
high = WebDriverWait(driver1, 10).until(EC.visibility_of_element_located((By.ID, "Label3"))).text
// you don't need to wait again here since the wait should guarantee the element is accessible
low = driver1.find_element_by_id("Label1").text
使用这种方法,你可以将低点与高点分开,我假设你无论如何都要做。这样可以避免字符串处理组合字符串......