1

I'm using selenium with chromedriver, and getting an error when trying to interact with a specific element.

HTML class code: <div class="label-31sIdr">Fresh</div>

Selenium Python code:

from selenium import webdriver
import requests
import time

path = '/Users/stryker/Downloads/chromedriver'
driver = webdriver.Chrome(path)

payload = {
    'content': "pls pm"
}

header = {
    'authorization': 'xxxxx'
}

for i in range (1000):
    r = requests.post('https://discord.com/api/v9/channels/991579455376072775/messages', data=payload, headers=header)
    time.sleep(1.5)
    button = driver.find_element_by_class_name('Fresh')
    button.click()
    time.sleep(30)

This is the error:

     line 19, in <module>
    button = driver.find_element_by_class_name('Fresh')
AttributeError: 'WebDriver' object has no attribute 'find_element_by_class_name'
2
  • You have not open browser by selenium, use driver.get(url) to open the page you want to click Fresh button. And class name is label-31sIdr. Commented Jun 29, 2022 at 9:29
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. Commented Jun 29, 2022 at 11:35

1 Answer 1

3

Selenium just removed that method in version 4.3.0. See the CHANGES: https://github.com/SeleniumHQ/selenium/blob/a4995e2c096239b42c373f26498a6c9bb4f2b3e7/py/CHANGES

Selenium 4.3.0
* Deprecated find_element_by_* and find_elements_by_* are now removed (#10712)
* Deprecated Opera support has been removed (#10630)
* Fully upgraded from python 2x to 3.7 syntax and features (#10647)
* Added a devtools version fallback mechanism to look for an older version when mismatch occurs (#10749)
* Better support for co-operative multi inheritance by utilising super() throughout
* Improved type hints throughout

You now need to use:

from selenium.webdriver.common.by import By

driver.find_element(By.CLASS_NAME, THE_CLASS_NAME)

Or without the extra import:

driver.find_element("class name", THE_CLASS_NAME)

For improved reliability, you should consider using WebDriverWait in combination with element_to_be_clickable.

The other issue is that you mentioned the HTML is: <div class="label-31sIdr">Fresh</div> but you are trying to use Fresh as the class name when label-31sIdr is the class name. The code for that would be:

driver.find_element("class name", "label-31sIdr")
Sign up to request clarification or add additional context in comments.

1 Comment

I didn't know about that update so, thank you Michael for the info but the reason i was using Fresh was cuz there are 4 classes and all name "label-31sIdr" only the name between div is different

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.