HenryM
HenryM

Reputation: 5793

Why can't selenium find a button in a Chrome Extension (Python)

I am using BeautifulSoup with Selenium. I'm opening a webpage with Selenium with a chrome extension. When I manually right click and 'inspect' on a button I can see the html

<button type="button" class="btn btn-primary" style="width: 150px;">Find Email</button>

If I right click and 'view page source' this html doesn't show up in the source code which I presume is why selenium cannot find it. It seems to be driven by json and javascript but I am not sure on the full workings. Can anyone suggest how I use selenium in this environment.

Here's how I'm opening the browser:

    options = webdriver.ChromeOptions()
    options.add_argument("--start-maximized")
    options.add_extension('/home/henry/Downloads/candidate.ai-get-email,-salary,-social-link_v0.3.6.crx')
    with closing(Chrome(chrome_options=options)) as driver:
        driver.get(url)

Here's how I'm trying to get the button:

    button = driver.find_element_by_css_selector('btn')
    button.click()

Here's the error I'm given:

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

Upvotes: 1

Views: 1337

Answers (1)

Andersson
Andersson

Reputation: 52665

As btn is not an element, but a class name, you cannot use it in your way. Some of possible ways you can use class name are:

button = driver.find_element_by_css_selector('.btn')
button = driver.find_element_by_css_selector('button.btn')
button = driver.find_element_by_css_selector('button.btn.btn-primary')
button = driver.find_element_by_xpath('//button[@class="btn btn-primary"]')
...

Also you can find yor button by it's link text:

button = driver.find_element_by_xpath('//button[.="Find Email"]')
button = driver.find_element_by_link_text("Find Email")

As your button located inside an iframe, use following code:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver.switch_to_frame("bconsole")
button = WebDriverWait(driver, 2).until(EC.element_to_be_clickable((By.LINK_TEXT, "Find Email")))
...
# Perform required actions inside iframe
driver.switch_to_default_content()

Upvotes: 1

Related Questions