Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 属性错误:';列表';对象没有属性';单击';使用Selenium和Python_Javascript_Python_Selenium_Selenium Webdriver_Webdriver - Fatal编程技术网

Javascript 属性错误:';列表';对象没有属性';单击';使用Selenium和Python

Javascript 属性错误:';列表';对象没有属性';单击';使用Selenium和Python,javascript,python,selenium,selenium-webdriver,webdriver,Javascript,Python,Selenium,Selenium Webdriver,Webdriver,我想在默认设置为“季度”的页面上单击“年度”按钮。有两个链接基本上被称为相同的,只是其中一个有data ptype=“Annual”,所以我尝试复制xpath来单击按钮(也尝试了其他选项,但没有一个有效) 但是,我得到的属性错误:“list”对象没有属性“click”。我读了很多类似的帖子,但没能解决我的问题。。所以我假设javascript事件必须以不同的方式调用/单击/执行。。我卡住了 from selenium import webdriver link = 'https://www.in

我想在默认设置为“季度”的页面上单击“年度”按钮。有两个链接基本上被称为相同的,只是其中一个有
data ptype=“Annual”
,所以我尝试复制xpath来单击按钮(也尝试了其他选项,但没有一个有效)

但是,我得到的
属性错误:“list”对象没有属性“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>

您需要使用
find\u element\u by\u xpath
而不是
find\u element\u by\u xpath
返回
列表

driver.find_element_by_xpath("/html/body/div[5]/section/div[8]/div[1]/a[1]").click()
我也认为最好用这个例子

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()

您需要使用
find_element_by_xpath
而不是
find_element_by_xpath
返回
列表

driver.find_element_by_xpath("/html/body/div[5]/section/div[8]/div[1]/a[1]").click()
我也认为最好用这个例子

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()

请注意,
find\u elements\u by\u xpath
是复数形式,它返回元素列表。不止一个。该列表可以不包含、只包含一个或多个元素

例如,您可以单击第一个匹配项:

driver.find_elements_by_xpath("/html/body/div[5]/section/div[8]/div[1]/a[1]")[0].click()

请注意,
find\u elements\u by\u xpath
是复数形式,它返回元素列表。不止一个。该列表可以不包含、只包含一个或多个元素

例如,您可以单击第一个匹配项:

driver.find_elements_by_xpath("/html/body/div[5]/section/div[8]/div[1]/a[1]")[0].click()
根据文档,如果找到元素,则返回包含元素的列表,否则返回空列表。Python的列表没有与之关联的click()方法。而是将click()方法与其关联。因此,您必须使用
find\u element\u by\u xpath(xpath)
方法,通过与set的连接来诱导服务员,如下所示:

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
根据文档,如果找到元素,则返回包含元素的列表,否则返回空列表。Python的列表没有与之关联的click()方法。而是将click()方法与其关联。因此,您必须使用
find\u element\u by\u xpath(xpath)
方法,通过与set的连接来诱导服务员,如下所示:

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

我仍然建议您使用linkText而不是XPATH。原因:xpath:
/html/body/div[5]/section/div[8]/div[1]/a[1]
是绝对的,如果从html中添加了一个或删除了一个div,则可能会失败。而更改链接文本的机会非常小

因此,与此代码不同的是:

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\u element
,对web元素列表使用
find\u elements
。有
显式等待总是好的

创建显式等待的实例,如下所示:

wait = WebDriverWait(driver,20)
并按如下方式使用wait引用:

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 

我仍然建议您使用linkText而不是XPATH。原因:xpath:
/html/body/div[5]/section/div[8]/div[1]/a[1]
是绝对的,如果从html中添加了一个或删除了一个div,则可能会失败。而更改链接文本的机会非常小

因此,与此代码不同的是:

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\u element
,对web元素列表使用
find\u elements
。有
显式等待总是好的

创建显式等待的实例,如下所示:

wait = WebDriverWait(driver,20)
并按如下方式使用wait引用:

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 

对我来说,它不起作用,而且尝试了很多技巧,但都不起作用。有些人建议司机。隐式地等待(10)而不是时间。睡眠(10)不起作用。因此,请尝试在.click()代码行的上方和下方留出时间.sleep(10),并检查它是否有效。

对我来说,它不起作用,并且尝试了很多技巧,但都不起作用。有些人建议司机。隐式地等待(10)而不是时间。睡眠(10)不起作用。因此,请尝试在.click()代码行的上方和下方提供time.sleep(10),并检查它是否有效。

A谢谢,但我仍然收到一个错误,因为会出现一个弹出窗口。。有办法绕过它吗?原因:Element ClickInterceptedException:“元素在点(175.2999542236328530.3166961669922)处不可单击,因为另一个元素遮挡了它。”我更新我的答案,尝试添加
选项
以在整个窗口中打开
firefox
,但我在没有
选项的情况下运行代码,它工作正常,谢谢,但我仍然得到一个错误,因为弹出窗口显示。。有办法绕过它吗?原因:Element ClickInterceptedException:'元素在点(175.2999542236328530.3166961669922)处不可单击,因为另一个元素遮挡了它'我更新我的答案,尝试添加
选项
在整个窗口中打开
firefox
,但是我在没有
选项的情况下运行代码works@Felix:我已在更新部分更新了我的答案。这个代码在我的机器上运行良好。您必须向下滚动才能与年度按钮交互。谢谢@菲利克斯:我已经在更新部分更新了我的答案。这个代码在我的机器上运行良好。您必须向下滚动才能与年度按钮交互。谢谢工作很有魅力,谢谢!工作很有魅力,谢谢!这是评论,不是回答这是评论,不是回答