[英]Can not find div by class name via Selenium(Python)
I am trying to access the "events" inside the div with class name "rps-wrapper" within the url http://gridworlds-multiplayer.org/
but when I use the function I get an error.我试图在 url http://gridworlds-multiplayer.org/
访问类名“rps-wrapper”的 div 内的“事件”,但是当我使用该函数时,出现错误。
<div class="rps-wrapper">
<ul id="events"></ul>
<div class="controls">
<div class="chat-wrapper">
<form id="chat-form">
<input id="chat" autocomplete="off" title="chat"/>
<button id="say">Say</button>
</form>
</div>
</div>
</div>
<script src="/socket.io/socket.io.js"></script>
<script src="src/client.js"></script>
</body>
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('*the site is here*')
rps_wrapper = driver.find_element_by_class_name('rps-wrapper')
Should get the div with class name rps-wrapper, but outputs error elenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".rps-wrapper"} (Session info: chrome=75.0.3770.142)
应该得到类名 rps-wrapper 的 div,但输出错误elenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".rps-wrapper"} (Session info: chrome=75.0.3770.142)
To locate the events within the <div>
with class name rps-wrapper as the desired element is within an <frame>
so you have to:要在<div>
使用类名rps-wrapper定位事件,因为所需元素位于<frame>
因此您必须:
You can use either ofthe following Locator Strategies :您可以使用以下任一定位器策略:
Using CSS_SELECTOR
:使用CSS_SELECTOR
:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.TAG_NAME,"frame"))) rps_wrapper = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.rps-wrapper>ul#events")))
Using XPATH
:使用XPATH
:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.TAG_NAME,"frame"))) rps_wrapper = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='rps-wrapper']/ul[@id='events']")))
Note : You have to add the following imports :注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
Here you can find a relevant discussion on Ways to deal with #document under iframe在这里你可以找到关于Ways to deal with #document under iframe的相关讨论
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.