简体   繁体   English

使用 selenium python 访问内部元素

[英]accessing an element in <html> inside <html> with selenium python

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:这个网站有一个奇怪的html 正文,我确实设法在新标签中打开了第二个 html 代码,但它不是我想要的,不幸的是我试图用 Z8E00596AD8DE2213FF8F8D8478D5 自动点击它,但它似乎无法访问以下选择器:

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在此处输入图像描述

Something like this should work (assuming you have only 1 iframe, or the occurrence in question is the first iframe):像这样的东西应该可以工作(假设你只有 1 个 iframe,或者有问题的出现是第一个 iframe):

iframe is a subset of the main DOM. iframe 是主 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.在这种情况下,您首先需要先将 selenium 指针切换到 iframe,当指针位于 iframe 中时,然后访问 iframe 中的元素,然后访问 iframe 中的元素,然后访问 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:然后切换回父 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.注意:我不知道指针是否是正确的术语,但我认为它有助于让你用口语理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM