简体   繁体   English

如何在 Python(和 Selenium)中的下一条语句之前等待 for 循环完成?

[英]How to wait for the for loop to finish before next statement in Python (and Selenium)?

I have a list of 10 elements.我有一个包含 10 个元素的列表。 When I find a match in the list I want to click that element.当我在列表中找到匹配项时,我想单击该元素。 If no element matches I want to go to the "next page".如果没有元素匹配我想 go 到“下一页”。 (It is actually the same page but new async list elements is loaded in the list. Both lists has the same xpath.) Then I want to search the new page and click a element if it matches. (实际上是同一个页面,但是列表中加载了新的异步列表元素。两个列表具有相同的 xpath。)然后我想搜索新页面并单击一个元素,如果它匹配。 Here is the code:这是代码:

for xpath in list: # loop through elements
    element = WebDriverWait(driver, 40).until(EC.presence_of_all_elements_located((By.XPATH, xpath)))
    element = driver.find_element_by_xpath(xpath) # element fresh each time! no staleness here
    match = re.search(patternForFindingListItem, element.text) #if match then click on the element/item
    if match:
        element.click()
        break
    
if match == None: #want to go to next 10 items (notes) on "next page" (it is the same page but new loaded data via JavaScript) via next-button to see if there is a match there
    elementButton = driver.find_element_by_xpath(xpathNextButton)
    elementButton.click()

The problem is that it gets to the "next page" before the for loop is finished on the "first page".问题是它在 for 循环在“第一页”上完成之前到达“下一页”。 How can we wait for the loop to finish before going to the next page?在进入下一页之前,我们如何等待循环完成?

There are several possible problems here:这里有几个可能的问题:

  1. WebDriverWait(driver, 40).until(EC.presence_of_all_elements_located((By.XPATH, xpath))) will not wait untill all the elements matching xpath are presente. WebDriverWait(driver, 40).until(EC.presence_of_all_elements_located((By.XPATH, xpath)))不会等到所有匹配xpath的元素都出现。 It will return once at least one match found.一旦找到至少一个匹配项,它将返回。
  2. element = driver.find_element_by_xpath(xpath) according to the previous code line I guess you are trying to get the list of elements here, not a single element. element = driver.find_element_by_xpath(xpath)根据前面的代码行我猜你想在这里获取元素列表,而不是单个元素。 If so you should use find_elements_by_xpath here, so it will be elements = driver.find_elements_by_xpath(xpath)如果是这样,您应该在这里使用find_elements_by_xpath ,所以它将是elements = driver.find_elements_by_xpath(xpath)
  3. It's unclear what are you doing here: re.search(patternForFindingListItem, element.text) ...目前还不清楚你在这里做什么: re.search(patternForFindingListItem, element.text) ...

暂无
暂无

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

相关问题 在下一个循环之前等待 Python 完成 - Wait for Python to finish before next loop Python:在继续循环之前等待进程完成? - Python: Wait for process to finish before proceeding in loop? Selenium.click() 在执行下一个代码之前等待单击的元素完成加载。 如何不等待这个特殊问题? - Selenium .click() waits for the clicked element to finish loading before executing the next code. How not to wait for this particular problem? Python子进程:在开始下一个命令之前等待命令完成? - Python subprocess: wait for command to finish before starting next one? Selenium Python:在使用读取值之前,如何等待元素完成文本更改? - Selenium Python: How to wait until an element finish changing the text before reading the value using? Python 在 for 循环中的 os.system:如何在重新启动循环之前等待命令完成? - Python's os.system within a for loop: how to wait for a command to finish before re-starting the loop? Python-For Loop if语句跳至Selenium中的下一个迭代 - Python - For Loop if statement skip to next iteration in Selenium Python Selenium如何在点击链接之前等待 - Python Selenium how to wait before clicking on link 我不希望我的程序等待函数完成后再继续执行Python中的下一个任务 - I don't want my program to wait for a function to finish before moving on to the next task in Python 在继续Python之前,等待子进程.exe完成 - Wait for subprocess .exe to finish before proceeding in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM