简体   繁体   English

Python Selenium。 并行 if 循环

[英]Python Selenium. Parallel if loop

import csv
import time
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
from csv import reader
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import NoSuchElementException


chrome_options = Options()
scroll = 5
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
header_added = False
header_added1 = False
url = "url"
driver = webdriver.Chrome(executable_path='C:/chromedriver.exe', options=chrome_options)
driver.maximize_window()
driver.get(url)
time.sleep(3)
search_city = input("Enter the city :")
res_n = input("Enter the Restaurant's name :")
search = driver.find_element_by_xpath('//input[@name="location"]').send_keys(search_city)
time.sleep(2)
driver.find_element_by_xpath('//*[@id="root"]/div[1]/div[1]/div/div[1]/div[1]/div/div[2]/div/div[3]/div[1]/span[2]').click()
time.sleep(3)
driver.find_element_by_xpath('/html/body/div[1]/div[1]/header/div/div/ul/li[5]/div/a/span[1]').click()
time.sleep(1)
search_res = driver.find_element_by_class_name('_2BJMh').send_keys(res_n.lower())
time.sleep(5)
driver.find_element_by_class_name('_2BJMh').send_keys(Keys.RETURN)
time.sleep(5)

try:
    driver.find_element_by_class_name('_3FR5S').click()
    time.sleep(5)
except:
    print("restaurant not open")
    driver.quit()

html = driver.find_element_by_tag_name('html')



def get_items():
    global header_added
    global item_dvs
    cats = driver.find_elements_by_class_name('D_TFT')
    cats[1].click()
    time.sleep(3)
    item_dvs = driver.find_elements_by_class_name('_2wg_t')

    for div in item_dvs:
        name = div.find_element_by_class_name('styles_itemNameText__3bcKX')
        print(name.text)
        price = div.find_element_by_class_name('rupee')
        print(price.text)
        if div.find_elements_by_class_name('styles_itemDesc__MTsVd'):
            desc = div.find_element_by_class_name('styles_itemDesc__MTsVd').text
        else:
            desc = None
        if div.find_element_by_css_selector('div._1C1Fl._23qjy'):
            element = div.find_element_by_css_selector('div._1C1Fl._23qjy')
            print("found")
            driver.execute_script("arguments[0].scrollIntoView();", element)
            add = div.find_element_by_css_selector('._1RPOp')
            driver.execute_script("arguments[0].click();", add)
            time.sleep(1)
            add_ons = driver.find_element_by_class_name('_3UzO2').text
            print(add_ons)
            driver.find_element_by_css_selector('#modal-placeholder > div:nth-child(3) > div > div._1Kr-y._3EeZR > div > div._1EZLh > div > button').click()

        else:
            add_ons = None
        dict1 = {'Item Name': name.text, "Price": price.text, "Add Ons :": add_ons, "Description": desc}
        with open(f'{search_city}_{res_n}.csv', 'a+', encoding='utf-8-sig') as f:
            w = csv.DictWriter(f, dict1.keys())
            if not header_added:
                w.writeheader()
                header_added = True
            w.writerow(dict1)


get_items()

The is_cust loop keeps running over and over again opening the same element, while the rest of the code moves on to the next divs . is_cust循环不断运行,一遍又一遍地打开相同的元素,而代码的 rest 继续运行到下一个divs What is wrong here?这里有什么问题?

xPath are bidirectional and is probably the cause here. xPath 是双向的,可能是这里的原因。

Try this code using cssSelector:使用 cssSelector 尝试此代码:

for div in item_dvs:
    #Do Something

    try:   
        is_cust = div.find_element_by_css_selector('._1C1Fl._23qjy')
        print("found")
    except NoSuchElementException:
        continue

    driver.execute_script("arguments[0].scrollIntoView();", is_cust)
    add = div.find_element_by_css_selector('._1RPOp')
    driver.execute_script("arguments[0].click();", add)
    time.sleep(1)
    # Not sure why for this one you had driver instead of div. Suspect div should be 
    add_ons = div.find_element_by_class_name('_26cJ9').text
    div.find_element_by_css_selector('#modal-placeholder > div:nth-child(3) >   div > div._1Kr-y._3EeZR > div > div._1EZLh > div > button').click()

UPDATE From your updated code, you are using lot of hardcoded sleep.更新从您更新的代码中,您正在使用大量的硬编码睡眠。 I will suggest to use the WebDriverWait with expected_conditions .我会建议将WebDriverWaitexpected_conditions一起使用。

More info here: Wait from Selenium更多信息在这里:等待 Selenium

Imports needed:需要进口:

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

Code to be added post driver creation:创建驱动程序后要添加的代码:

wait_time = 5 wait = WebDriverWait(driver, wait_time) wait_time = 5 等待 = WebDriverWait(驱动程序,wait_time)

Instead of using sleep like this:而不是像这样使用睡眠:

time.sleep(5)
driver.find_element_by_class_name('_2BJMh').send_keys(Keys.RETURN)
time.sleep(5)

Use:利用:

wait.until(EC.presence_of_element_located((By.CLASS_NAME, '_2BJMh'))).send_keys(res_n.lower())

Don't gather the element twice.. use find_elements_by* then validate the length:不要两次收集元素..使用find_elements_by*然后验证长度:

descs = wait.until(EC.presence_of_all_elements_located((By.CLASS_NAME, 'styles_itemDesc__MTsVd')))
if len(descs) > 0:
    desc = descs[0].text
else:
    desc = None

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM