我想在默认设置为"季度"的页面上单击"年度"按钮。有两个链接基本上是一样的,只是其中一个有data-ptype="Annual"
,所以我尝试复制xpath来单击按钮(也尝试了其他选项,但都不起作用(。
然而,我得到了AttributeError: 'list' object has no attribute 'click'
。我读了很多类似的帖子,但没能解决我的问题。。所以我认为javascript事件必须以不同的方式调用/单击/执行。。我被卡住了
from selenium import webdriver
link = 'https://www.investing.com/equities/apple-computer-inc-balance-sheet'
driver = webdriver.Firefox()
driver.get(link)
elm = driver.find_elements_by_xpath("/html/body/div[5]/section/div[8]/div[1]/a[1]").click()
html如下:
<a class="newBtn toggleButton LightGray" href="javascript:void(0);" data-type="rf-type-button" data-ptype="Annual" data-pid="6408" data-rtype="BAL">..</a>
您需要使用返回list
的find_element_by_xpath
而不是find_elements_by_xpath
driver.find_element_by_xpath("/html/body/div[5]/section/div[8]/div[1]/a[1]").click()
此外,我认为最好使用Waits。
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.webdriver.firefox.options import Options
options = Options()
options.add_argument("--window-size=1920,1080")
driver = webdriver.Firefox(firefox_options=options)
path = "/html/body/div[5]/section/div[8]/div[1]/a[1]"
try:
element = WebDriverWait(driver, 5).until(
EC.element_to_be_clickable((By.XPATH, path)))
element.click()
finally:
driver.quit()
我仍然建议您使用linkText而不是XPATH。此xpath的原因:/html/body/div[5]/section/div[8]/div[1]/a[1]
是绝对的,如果添加了一个或从HTML中删除了,则可能失败。而更改链接文本的可能性非常小。
因此,代替这个代码:
elm = driver.find_elements_by_xpath("/html/body/div[5]/section/div[8]/div[1]/a[1]").click()
试试这个代码:
annual_link = driver.find_element_by_link_text('Annual')
annual_link.click()
是的@Druta是对的,对一个web元素使用find_element
,对web元素列表使用find_elements
。并且具有CCD_ 9总是好的。
创建显式等待的实例,如下所示:
wait = WebDriverWait(driver,20)
并使用这样的等待参考:
wait.until(EC.elementToBeClickable(By.LINK_TEXT, 'Annual'))
更新:
from selenium import webdriver
link = 'https://www.investing.com/equities/apple-computer-inc-balance-sheet'
driver = webdriver.Firefox()
driver.maximize_window()
wait = WebDriverWait(driver,40)
driver.get(link)
driver.execute_script("window.scrollTo(0, 200)")
wait.until(EC.element_to_be_clickable((By.LINK_TEXT, 'Annual')))
annual_link = driver.find_element_by_link_text('Annual')
annual_link.click()
print(annual_link.text)
确保导入这些:
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
根据文档,find_elements_by_xpath(xpath(返回一个列表,如果找到元素,则返回一个空列表。Python的List没有与其关联的click((方法。相反,find_element_by_xpath(xpath(方法有与其关联的clicking((方法。因此,您必须使用find_element_by_xpath(xpath)
方法通过WebDriverWait函数诱导服务员,并将expected_conditions设置为element_to_be_clickleable(定位器(,如下所示:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='newBtn toggleButton LightGray' and @data-type='rf-type-button']"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
注意,find_elements_by_xpath
是复数,它返回元素的列表。不仅仅是一个。该列表可以不包含任何元素,也可以只包含一个或多个元素。
例如,您可以点击第一个匹配:
driver.find_elements_by_xpath("/html/body/div[5]/section/div[8]/div[1]/a[1]")[0].click()
或者遍历列表并单击所有这些元素,或者可以使用find_element_by_xpath
(如果可以找到,则返回单个元素(:
driver.find_element_by_xpath("/html/body/div[5]/section/div[8]/div[1]/a[1]").click()
对我来说,它不起作用,我尝试了很多技巧,但都不起作用。有些人推荐driver.implicitly_wait(10(而不是time.sleep(10(,因为它不起作用。因此,请尝试在.click((代码行的上方和下方都给.sleep(10(时间,并检查它是否有效。