Jack
Jack

Reputation: 1

Selenium find element error, but same code with js can work

use the python execute can find the element bottom and click, it works:

driver.execute_script("document.getElementsByClassName(\"g-c-R  webstore-test-button-label\")[0].click()")

but with the similar code, the python code can not work:

element_install_bottom=driver.find_element(by=By.ID, value=r'g-c-R  webstore-test-button-label')

and throw the exception:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="g-c-R  webstore-test-button-label"]"}

and have try by.ID, by.ClassName; but always throw the exception.

the whole code as below: from selenium import webdriver from selenium.webdriver.common.by import By import selenium.webdriver.support.ui as ui

import time

option = webdriver.ChromeOptions()

USER_DATA_PATH = r"D:\Chrome\User Data 3"
option.add_argument(f'--user-data-dir={USER_DATA_PATH}')
option.add_experimental_option('excludeSwitches', ['enable-automation'])

print(option.arguments)


driver = webdriver.Chrome(options=option)

extension_url = "https://chrome.google.com/webstore/detail/dark-reader/eimadpbcbfnmbkopoojfekhnkhdbieeh?hl=zh-CN"
driver.get(extension_url)
time.sleep(60)


element_install_bottom=driver.find_element(by=By.ID, value=r'g-c-R  webstore-test-button-label')
print(element_install_bottom)

Upvotes: 0

Views: 45

Answers (1)

Luth
Luth

Reputation: 11

You're seeing that exception as there is no element with that ID, that value is it's class.

Python:

driver.find_element_by_class_name("g-c-R  webstore-test-button-label")

or:

driver.find_element(By.CLASS_NAME, "g-c-R  webstore-test-button-label")

Upvotes: 1

Related Questions