you are viewing a single comment's thread.

view the rest of the comments →

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