如何在2023年正确使用WebDriverWait和presence_of_element_located()?

huangapple go评论57阅读模式
英文:

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'

huangapple
  • 本文由 发表于 2023年2月19日 10:15:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497593.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定