Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 尽管在技术上不可能,但语句仍在循环_Python_Python 3.x_Selenium_Selenium Webdriver_Geckodriver - Fatal编程技术网

Python 尽管在技术上不可能,但语句仍在循环

Python 尽管在技术上不可能,但语句仍在循环,python,python-3.x,selenium,selenium-webdriver,geckodriver,Python,Python 3.x,Selenium,Selenium Webdriver,Geckodriver,我正在编写一个Selenium Python脚本,它应该从所有页面中删除所有超链接,并使用单击的“next”按钮在它们之间切换。这将成功地刮除所有链接,但当它到达最后一页时,“下一步”按钮元素将不再存在,它将继续在最后一页上循环,并将刮除的数据一次又一次地写入CSV文件 就我对while和try/except语句的设置所知,这在技术上是不可能的。几个小时来我一直在乱搞代码,结果头发掉了,但我还是没能把它修好 这是我试图从中获取信息的网站: 如您所见,有红色的公司名称,底部有“下一步”箭头按钮。

我正在编写一个Selenium Python脚本,它应该从所有页面中删除所有超链接,并使用单击的“next”按钮在它们之间切换。这将成功地刮除所有链接,但当它到达最后一页时,“下一步”按钮元素将不再存在,它将继续在最后一页上循环,并将刮除的数据一次又一次地写入CSV文件

就我对while和try/except语句的设置所知,这在技术上是不可能的。几个小时来我一直在乱搞代码,结果头发掉了,但我还是没能把它修好

这是我试图从中获取信息的网站:

如您所见,有红色的公司名称,底部有“下一步”箭头按钮。这是我的代码,应该删除所有链接:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import WebDriverException, TimeoutException
from platform import system
from os import getcwd, getlogin
import csv

wait = WebDriverWait(driver, 10)

with open('links.csv', 'w+', newline='') as write:
    driver.get("https://www.sreality.cz/adresar")
    writer = csv.writer(write)
    page_spawn = 0
    while page_spawn == 0:
        try:
            links = wait.until(ec.presence_of_all_elements_located((By.CSS_SELECTOR, "h2.title > a")))
            #print(len(links))
            for link in links:
                print(link.get_attribute("href"))
                writer.writerow([link.get_attribute("href")])
            wait.until(ec.element_to_be_clickable((By.CSS_SELECTOR, "a.btn-paging-pn.icof.icon-arr-right.paging-next"))).click()
        except TimeoutException:
            page_spawn = 1
            break

您没有更改try块中的
page\u spawn
值,这可能是循环n次的原因。

该箭头按钮元素仍然存在,但已被禁用:

>> window.location
Location https://www.sreality.cz/adresar?strana=152

>> document.querySelector("a.btn-paging-pn.icof.icon-arr-right.paging-next")
<a class="btn-paging-pn icof icon-…ht paging-next disabled" ng-href="" ng-class="{disabled: !pagingData.nextUrl}">
在仍与非禁用元素匹配时:

>> window.location
Location https://www.sreality.cz/adresar?strana=152

>> document.querySelector("a.btn-paging-pn.icof.icon-arr-right.paging-next:not(.disabled)")
null
>> window.location
Location https://www.sreality.cz/adresar?strana=151

>> document.querySelector("a.btn-paging-pn.icof.icon-arr-right.paging-next:not(.disabled)")
<a class="btn-paging-pn icof icon-arr-right paging-next" ng-href="/adresar?strana=152" ng-class="{disabled: !pagingData.nextUrl}" href="/adresar?strana=152">
>window.location
位置https://www.sreality.cz/adresar?strana=151
>>document.querySelector(“a.btn-paging-pn.icof.icon arr right.paging next:not(.disabled)”)

您的
除非
只有在您点击
超时异常时才会触发
。你确定代码会引发该异常,而不是其他异常,比如
NoTouchElementException
?据我所知,它会引发TimeoutException,因为过去其他“突然丢失”元素也是如此。但是,即使我做了如下操作:除异常为e:page_spawn=1 break外,结果仍然是相同的不幸的另一件事需要检查:您说“next”按钮元素不应该再存在,但您是否检查了页面源以确保该元素实际上不存在,并不是说它存在但不可见或不可点击?这实际上是完全有道理的!我知道该元素已禁用,但没有意识到选择器仍会在禁用后拾取它。无论如何,我认为使用:not是一个完美的解决方案。谢谢你让我知道这件事!