for循环执行

时间:2018-12-27 05:02:27

标签: python selenium for-loop web

我有两个for循环。外循环从一个文本文件读取并进入另一个for循环,当出现异常时,该循环从另一个文本文件读取。内部循环应该退出循环,然后迭代到外部循环中的下一个元素,但是再次,一旦迭代,它应该从内部循环中停止的地方继续。

关于如何在python中执行操作的任何想法?

以下是代码:

with open('E:\marr.txt') as f:
    content = f.readlines()
content = [x.strip() for x in content]
with open('E:\prlist.txt') as f:
    content1 = f.readlines()
content1 = [x.strip() for x in content1]
with open('E:\Master\master1.csv', 'a') as f:
    headers = ("Source Link,Company Link,company name")
    f.write(headers)
    f.write("\n")
    for ip in content1:
        chrome_options.add_argument('--proxy-server=%s' % ip)
        try:
            for link in content:

                    try:
                        browser.get(link)
                        browser.implicitly_wait(4)
                        abba = browser.find_element_by_css_selector('.WebToolsetToolWebPart_Cntnr.WebToolsetToolWebPart_Aligned_LEFT.WebToolsetToolWebPart_TxtTool_Cntnr')
                        aas = abba.text
                        aa = aas.replace(",","")
                        print(ip + "," + link + "," + aa)
                        f.write(link + "," +aa+"\n")

                    except NoSuchElementException:
                        aa = "no count available"
                        print(ip + ","+link + "," + aa)
                        f.write(link + "," + aa + "\n")
        except StaleElementReferenceException:
            pass

1 个答案:

答案 0 :(得分:1)

保存内循环索引,如果继续出现错误,请参见public class ViewHolder extends RecyclerView.ViewHolder{ private View YOUR_VIEW; public ViewHolder(@NonNull View itemView) { super(itemView); YOUR_VIEW = itemView.findViewById(R.id.YOUR_VIEW); YOUR_VIEW.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(mContext,"Position==>> "+getAdapterPosition(),Toast.LENGTH_LONG).show(); } }); } }

currentIndex