x-Palace
x-Palace

Reputation: 43

Python Unable to locate element

I'm trying to click a button. I tried many methods but did not work

So my question is, How can i click the "i agree" button?

plaese see image

I tried this method

First:

browser.switch_to.frame(browser.find_element_by_id('introAgreeButton'))
vote = browser.find_element_by_xpath('//*[@id="introAgreeButton"]')
vote.click()

Second:

accepted = browser.find_elements_by_id('introAgreeButton')
browser.execute_script("arguments[0].click();", accepted )

Third(Classic):

accepted = browser.find_elements_by_id('introAgreeButton')
accepted.click()

Upvotes: 0

Views: 283

Answers (2)

Virtual
Virtual

Reputation: 21

The above reply from sykezlol didn't work for me, but this code did:

            browser.switch_to_default_content()
            browser.switch_to.frame(browser.find_elements_by_tag_name('iframe')[0])
            browser.implicitly_wait(1)
            browser.find_element_by_id('introAgreeButton').click()
            browser.switch_to.default_content()

I'm using 'browser' instead of 'driver' here.

Upvotes: 1

sykezlol
sykezlol

Reputation: 88

You can use full xpath to get the iframe's full path. I got your script working like this:

driver.get('https://www.google.com')


driver.switch_to.frame(driver.find_element_by_xpath('/html/body/div/div[4]/div[3]/div/div[2]/span/div/div/iframe'))
element = driver.find_element_by_id('introAgreeButton')

element.click()

As you can see, I am using driver.switch_to.frame - inside this frame switch I will find the iframe element by using full xpath. You can find the full path by right clicking when inspecting it and selection "Copy -> Full Xpath"

I hope this helps, otherwise please ask :)

Upvotes: 0

Related Questions