简体   繁体   English

使用 selenium 和 python 按 class 名称单击按钮

[英]Clicking a button by class name using selenium with python

Probably a silly question, but I have spent a ridiculous amount of time trying to figure this out.可能是一个愚蠢的问题,但我花了很多时间来弄清楚这个问题。 I am building a scrapper bot using selenium in python, and I am just trying to click a button on a web page.我正在 python 中使用 selenium 构建一个 scrapper bot,我只是想点击 web 页面上的一个按钮。 The web page opens and resizes... web 页面打开并调整大小...

def initalize_browser():
driver.get("**website name**")
driver.maximize_window()

but I cannot get it to click a specific button.但我无法让它点击特定按钮。 This is the buttons HTML code:这是按钮 HTML 代码:

<button class="mx-auto green-btn btnHref" onclick="window.location ='/medical'" onkeypress="window.location='/medical'">
                            Medical and Hospital Costs
                        </button>

And this is my code:这是我的代码:

 click_button=driver.find_element(by=By.CLASS_NAME, value="mx-auto green-btn btnHref")

 click_button.click()

This is the error I get for this code:这是我为这段代码得到的错误:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".mx-auto green-btn btnHref"}

I have tried out so many variations of this, including:我已经尝试了很多这样的变体,包括:

 driver.find_element_by_xpath('//button[@class="mx-auto green-btn btnHref"]').click()

Where I get this error:我在哪里收到此错误:

AttributeError: 'WebDriver' object has no attribute 'find_element_by_xpath'

I have also checked to see if there are perhaps any other attributes with the same class name, but there is not.我还检查了是否有任何其他属性具有相同的 class 名称,但没有。 Any help would be super appreciated, thank you!任何帮助将不胜感激,谢谢!

The method find_element_by_xpath is deprecated now.现在不推荐使用find_element_by_xpath方法。 Use this line:使用这一行:

driver.find_element(By.XPATH, '//button[@class="mx-auto green-btn btnHref"]').click()

instead of:代替:

driver.find_element_by_xpath('//button[@class="mx-auto green-btn btnHref"]').click()

And be sure you have this in imports:并确保你在进口中有这个:

from selenium.webdriver.common.by import By

The locator click_button=driver.find_element(by=By.CLASS_NAME, value="mx-auto green-btn btnHref") doesn't work because By.CLASS_NAME needs only one class name to find an element, but you gave it 3 class names.定位器click_button=driver.find_element(by=By.CLASS_NAME, value="mx-auto green-btn btnHref")不起作用,因为By.CLASS_NAME只需要一个 class 名称来查找元素,但您给了它 3 class名字。 The html attribute class consists of a list of elements divided by space. html 属性class由按空格分隔的元素列表组成。 So, in this html code所以,在这个 html 代码中

<button class="mx-auto green-btn btnHref" onclick="window.location ='/medical'" onkeypress="window.location='/medical'">
                            Medical and Hospital Costs
                        </button>

the attribute class has 3 class names mx-auto , green-btn and btnHref属性class有 3 个 class 名称mx-autogreen-btnbtnHref
You can't use all the 3 classes with By.CLASS_NAME but you can use all of them using the By.XPATH您不能将所有 3 个类与By.CLASS_NAME一起使用,但您可以使用By.XPATH使用所有这些类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM