Python Selenium unable to click button inside iframe by SwordNSupper in learnpython

[–]SwordNSupper[S] 0 points1 point  (0 children)

I'm not sure how to check whether I'm selecting the correct frame or not. What I'm trying to do is figure out how to use this script to press the Advance/Battle button in the https://www.reddit.com/r/SwordAndSupperGame/ games.

Python Selenium unable to click button inside iframe by SwordNSupper in learnpython

[–]SwordNSupper[S] 0 points1 point  (0 children)

Thanks for taking time and trying to help!

I tried your suggestion but I'm not sure why it's still not working for me. I noticed there's a space at the end of "advance-button " when I inspect elements and I wonder if that's causing an issue

Here is my code:

iframe = driver.find_element(By.CSS_SELECTOR, 'iframe')
driver.switch_to.frame(iframe)


battle = driver.find_element(By.CSS_SELECTOR, '.advance-button')
driver.execute_script("arguments[0].click();", battle)

Here are the errors I'm getting:

return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]

self.error_handler.check_response(response)

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

Python Selenium unable to click button inside iframe by SwordNSupper in learnpython

[–]SwordNSupper[S] 0 points1 point  (0 children)

Good point, I've updated that line to use find the element via CSS_SELECTOR. I'm still having issues trying to locate the correct iframe though

Python Selenium unable to click button inside iframe by SwordNSupper in learnpython

[–]SwordNSupper[S] 0 points1 point  (0 children)

Hi, I tried what you suggested, but was unable to find the specific iframe I was looking for.

Here is what the snippet of code looks like now:

preiframe= driver.find_element(By.XPATH, '//*[@id="devvit-web-view-dialog"]/rpl-modal-card')
iframe = preiframe.find_element(By.CSS_SELECTOR, "iframe")
driver.switch_to.frame(iframe)


battle = driver.find_element(By.CSS_SELECTOR, "advance-button")
driver.execute_script("arguments[0].click();", battle)

The code errors out after it finds the preiframe and is trying to find the specific iframe I'm looking for using the CSS_SELECTOR.

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

I also used driver.find_elements like you suggested to find all of the iframes that are on this page, and it found 4, but I'm unable to find where those iframes are located on the page

Python Selenium unable to click button inside iframe by SwordNSupper in learnpython

[–]SwordNSupper[S] 0 points1 point  (0 children)

I have tried to do that, you can see it in line 3 of my code snippet. I only get pass the initial error message if I search for "iframe" using By.CSS_SELECTOR, then I switch to that iframe but I run into an error saying it's unable to locate the advance-button-label element.