英文:
How to correctly use WebDriverWait & presence_of_element_located() in 2023?
问题
这是我检测到的HTML:
<div class="arrowPopup arrowPopup-start">
<div class="arrowPopupText arrowPopupTextTwoLine arrowPopupText-flashOn" style="white-space: nowrap;">type<br>this</div>
</div>
这是我的代码:
element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CLASS_NAME, 'arrowPopup arrowPopup-start')))
这不起作用,在20秒后超时了。有帮助吗?
英文:
Here is the HTML I am detecting:
<div class="arrowPopup arrowPopup-start">
<div class="arrowPopupText arrowPopupTextTwoLine arrowPopupText-flashOn" style="white-space: nowrap;">type<br>this</div>
</div>
Here is my code
element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CLASS_NAME, 'arrowPopup arrowPopup-start')))
This does not work and times out after 20 seconds.
Any help?
答案1
得分: 1
Sure it does. This is straight out of the docs,
element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "myDynamicElement"))
My guess is that your locator is incorrect or the element is in an IFRAME but we don't have enough info to determine for sure.
wait = new WebDriverWait(driver, 20) # may need to adjust this based on how much time it takes to get a full game
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[text()='Enter a Typing Race']"))).click();
textToType = wait until(EC.visibility_of_element_located((By.XPATH, "//div[./span[@unselectable]]"))).text;
typeHere = wait until(EC.visibility_of_element_located((By.XPATH, "//input[contains(@class,'txtInput')][not(@disabled)]")));
for character in textToType
typeHere.send_keys(character);
Thread.Sleep(50); # adjust this to change the typing speed
Since you asked for a breakdown of this XPath
//input[contains(@class,'txtInput')][not(@disabled)]
^ starting at the top of the DOM, find any INPUT
^ whose class contains the text 'txtInput'
^ but does NOT contain the attribute 'disabled'
英文:
Sure it does. This is straight out of the docs,
element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "myDynamicElement"))
My guess is that your locator is incorrect or the element is in an IFRAME but we don't have enough info to determine for sure.
wait = new WebDriverWait(driver, 20) # may need to adjust this based on how much time it takes to get a full game
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[text()='Enter a Typing Race']"))).click();
textToType = wait.until(EC.visibility_of_element_located((By.XPATH, "//div[./span[@unselectable]]"))).text;
typeHere = wait.until(EC.visibility_of_element_located((By.XPATH, "//input[contains(@class,'txtInput')][not(@disabled)]")));
for character in textToType
typeHere.send_keys(character);
Thread.Sleep(50); # adjust this to change the typing speed
Since you asked for a breakdown of this XPath
//input[contains(@class,'txtInput')][not(@disabled)]
^ starting at the top of the DOM, find any INPUT
^ whose class contains the text 'txtInput'
^ but does NOT contain the attribute 'disabled'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论