Home > Mobile >  accessing an element in <html> inside <html> with selenium python
accessing an element in <html> inside <html> with selenium python

Time:09-02

there is this website with a weird html body, i did manage to open the 2nd html code in a new tab but its not what i want unfortunately i am trying to auto click it with selenium but it seems to be inaccessible i've tried the following selectors:

vidstream = driver.find_elements(By.TAG_NAME, value='html')[1]
# error: list index out of range!
vidstream = driver.find_element(By.CLASS_NAME, value='._reload')
# error: no such element: Unable to locate element: {"method":"css selector","selector":"._reload"}
vidstream = driver.find_element(By.CSS_SELECTOR, value='.fullpage.vidplay._reload')
# erroe: no such element: Unable to locate element: {"method":"css selector","selector":".fullpage.vidplay._reload"}

and many other possibilities but with out any luck, any work around is welcome, thanks.

enter image description here

CodePudding user response:

Something like this should work (assuming you have only 1 iframe, or the occurrence in question is the first iframe):

iframe is a subset of the main DOM. It is not necessarily that all websites uses this, but some do; in such case, you first need to switch your selenium pointer to the iframe first, and when the pointer is in iframe, then access the elements inside the iframe.

driver.switch_to.frame(driver.find_element(By.TAG_NAME, 'iframe'))
driver.find_element(By.XPATH, "//div[@class='fullpage video _reload") # pls check if I have written the class name correctly.

And then to switch back to parent DOM:

driver.switch_to.default_content()

Note: I do not know if pointer is the right term, but I thought it helps in making you understand in a colloquial tongue.

  • Related