我正在尝试使用Selenium选择并单击网站中的一个按钮,但是有多个具有相同类的按钮

时间:2019-09-02 12:21:45

标签: python-3.x

我只是想玩Selenium,想知道我是否可以单击任何可单击的对象,但发现自己无法单击许多对象(按钮)。

到目前为止,我一直在尝试这些方法:

(By.XPATH, ('/html/body/div[1]/div/div[2]/div[1]/div/div[1]/div/div/div[26]'))
browser.find_element_by_link_text("Soccer").click()

这是我要单击的html代码,我要单击“足球”按钮。

<b>
    <div class="wn-Classification" style="">Rugby League<div class="wn-Classification_FavIcon "></div></div>
    <div class="wn-Classification">Rugby Union<div class="wn-Classification_FavIcon "></div></div>
    <div class="wn-Classification" style="">Snooker<div class="wn-Classification_FavIcon "></div></div>
    <div class="wn-Classification" style="">Soccer<div class="wn-Classification_FavIcon "></div></div>
</b>

错误:

问题是:Message: no such element: Unable to locate element: {"method":"link text","selector":"Soccer"}

有人可以帮助吗?

最吸引人的是以下if语句经过,然后说它找不到文件,但路径相同:

if EC.presence_of_element_located((By.XPATH, ('/html/body/div[1]/div/div[2]/div[1]/div/div[1]/div/div/div[26]'))):
                self.browser.find_element_by_xpath("/html/body/div[1]/div/div[2]/div[1]/div/div[1]/div/div/div[26]").click()
            else:
                print("hm")

2 个答案:

答案 0 :(得分:0)

问题是find_element在页面完全加载之前运行太快。尝试等待元素出现并设置计时器(在此示例中为10秒)。然后确保添加缺少的导入。

默认情况下,Webdriver将等待页面加载:

WebDriverWait(browser, 10).until(EC.presence_of_element_located(browser.find_element_by_link_text("Soccer")))

如果那不是问题,有两种方法可以做到这一点-

driver.find_elements_by_class_name('wn-Classification')[3].click()

或者你去

driver.find_element_by_link_text('Soccer').click()

答案 1 :(得分:0)

感谢“ 41 72 6c”的支持。 您一直以来都是对的,我只是继续您的想法并尝试了此操作:

Started:  15:16:56
Error: 2019-09-02 15:16:58.02
   Code: 0xC001F02A
   Source: Load Full PLU Files
   Description: Cannot create a task from XML for task "get header info", type "Microsoft.ScriptTask" due to error 0x80070057 "The parameter is incorrect.".
End Error
Error: 2019-09-02 15:16:58.03
   Code: 0xC0010018
   Source: get header info
   Description: Failed to load task "get header info", type "". The contact information for this task is "".
End Error
Error: 2019-09-02 15:16:58.32
   Code: 0xC0040019
   Source: load full updates load full updates (SSIS.Pipeline)
   Description: Data Flow objects cannot be loaded. Check if Microsoft.SqlServer.PipelineXml.dll is properly registered.
End Error
Error: 2019-09-02 15:16:58.32
   Code: 0xC0010018
   Source: load full updates
   Description: Failed to load task "load full updates", type "SSIS.Pipeline.7". The contact information for this task is "Performs high-performance data extraction, transformation and loading;Microso
ft Corporation; Microsoft SQL Server; (C) Microsoft Corporation; All Rights Reserved;http://www.microsoft.com/sql/support/default.asp;1".
End Error
Error: 2019-09-02 15:16:58.40
   Code: 0xC0010026
   Source: get header info
   Description: The task has failed to load. The contact information for this task is "".
End Error
Error: 2019-09-02 15:16:58.42
   Code: 0xC0024107
   Source: get header info
   Description: There were errors during task validation.
End Error
Error: 2019-09-02 15:16:58.42
   Code: 0xC0010025
   Source: importHQFullExtract
   Description: The package cannot execute because it contains tasks that failed to load.
End Error
DTExec: The package execution returned DTSER_FAILURE (1).
Started:  15:16:56
Finished: 15:16:58
Elapsed:  1.997 seconds

all the others give somethign like :

Microsoft (R) SQL Server Execute Package Utility
Version 15.0.1301.433 for 32-bit
Copyright (C) 2019 Microsoft. All rights reserved.

Started:  15:53:53
Could not create DTS.Application because of error 0x80040154
Started:  15:53:53
Finished: 15:53:53
Elapsed:  0.016 seconds

它奏效了。

再次感谢您。