Fel*_*lix 4 javascript python selenium webdriver selenium-webdriver
我想在默认设置为“季度”的页面上单击“年度”按钮。有两个基本上相同的链接,除了一个链接,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()
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
您需要使用find_element_by_xpath不find_elements_by_xpath返回一个list
driver.find_element_by_xpath("/html/body/div[5]/section/div[8]/div[1]/a[1]").click()
Run Code Online (Sandbox Code Playgroud)
我也认为最好使用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()
Run Code Online (Sandbox Code Playgroud)
我仍然建议您使用linkText而不是XPATH。原因是这个 xpath :是相当绝对的,如果在 HTML 中添加或删除了一个以上的 div/html/body/div[5]/section/div[8]/div[1]/a[1],则可能会失败。而更改链接文本的机会非常小。
所以,代替这段代码:
elm = driver.find_elements_by_xpath("/html/body/div[5]/section/div[8]/div[1]/a[1]").click()
Run Code Online (Sandbox Code Playgroud)
试试这个代码:
annual_link = driver.find_element_by_link_text('Annual')
annual_link.click()
Run Code Online (Sandbox Code Playgroud)
是的,@Druta 是对的,用于find_element一个 Web 元素和find_elementsWeb 元素列表。拥有它总是好的explicit wait。
像这样创建显式等待实例:
wait = WebDriverWait(driver,20)
Run Code Online (Sandbox Code Playgroud)
并像这样使用等待引用:
wait.until(EC.elementToBeClickable(By.LINK_TEXT, 'Annual'))
Run Code Online (Sandbox Code Playgroud)
更新:
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)
Run Code Online (Sandbox Code Playgroud)
确保导入这些:
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1592 次 |
| 最近记录: |