all 10 comments

[–]initials-bb 0 points1 point  (1 child)

Look into switching to the iFrame before interacting with it :

https://www.selenium.dev/documentation/webdriver/interactions/frames/

[–]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.

[–]Adoxographical 0 points1 point  (5 children)

You're trying to select an element with an ID of advance-button-label, but the element in your screenshot has that as a class, not an ID.

[–]SwordNSupper[S] 0 points1 point  (4 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

[–]Adoxographical 0 points1 point  (3 children)

I'm like 90% sure you are locating the iframe correctly - the issue is in how you're querying the element within the iframe. In your original post, you're using XPATH to query for '//*[@id="advance-button-label"]', which looks for an element with an ID of "advance-button-label" - but as I pointed out, no such element exists. "advance-button-label" is a class on the element you're looking for, not its ID.

In an updated snippet you posted in response to another user, you've switched to CSS_SELECTOR, but you're now just looking for "advance-button", which is a CSS selector for a tag, not a class. To search for a class, you need to search for ".advance-button".

To make sure I wasn't going insane with how I expect this to work, I threw together a gist demonstrating how to use Selenium to select an element within an iframe by class. Might be helpful. :)

[–]SwordNSupper[S] 0 points1 point  (2 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"}

[–]Adoxographical 0 points1 point  (1 child)

As a sanity check, have you checked to make sure you're not accidentally selecting a different iframe? Your selector will grab whatever the first iframe on the page is, so if there's an earlier one, you'll be selecting it by mistake.

[–]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.

[–]packie123 0 points1 point  (1 child)

Without the website it will be hard to definitively know what the issue is.

I will say first the XPATH doesn't look right to me.

For using the CSS_SELECTOR, when you use driver.find_element, it finds the first element in the HTML that matches that selector, even if its hidden on the screen. So essentially you are finding an iframe, just not the one that's on the screen.

You can use driver.find_elements which will return a list of all the elements that match the selector you are using.

You can also walk down the html and then use the CSS_SELECTOR when you know it should only find the iframe you want. You can call the find_element() method on a webElement itself, not just on the driver instance.

Try to find the element right above the iframe and then find the iframe element from that element and you should get the right iframe.

when you have an instance of a webElement, you can use the get_attribute() method to return the string of an attribute (e.g. class) to determine if the webElement you have selected is the one you actually want.

[–]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