Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.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 Selenium关于切换到iframe的帮助,然后单击链接_Python_Selenium_Web Scraping - Fatal编程技术网

Python Selenium关于切换到iframe的帮助,然后单击链接

Python Selenium关于切换到iframe的帮助,然后单击链接,python,selenium,web-scraping,Python,Selenium,Web Scraping,我目前正在尝试点击每个链接,这些链接可以为我提供更多关于餐馆的信息。 第一家餐厅是151社交餐厅,我正试图点击它,但似乎不适合我。我已经编译了一个pd.Series,其中包含指向每个餐厅的xpath链接 for element in range(len(df['Temp xPath links'])): frame = WebDriverWait(driver, 30).until(EC.frame_to_be_available_and_switch_to_it((By.ID

我目前正在尝试点击每个链接,这些链接可以为我提供更多关于餐馆的信息。

第一家餐厅是151社交餐厅,我正试图点击它,但似乎不适合我。我已经编译了一个pd.Series,其中包含指向每个餐厅的xpath链接

for element in range(len(df['Temp xPath links'])):    
    frame = WebDriverWait(driver, 30).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"iframe-form")))
    ref = WebDriverWait(driver, 30).until(EC.presence_of_element_located(By.xpath(df['Temp xPath links'][element])))
    driver.execute_script("arguments[0].scrollIntoView();", ref)
    driver.execute_script("arguments[0].click();", ref)
//*[@id=“lblContent”]/div[3]/table/tbody/tr[1]/td[1]/a

那只是第一家餐厅的

for element in range(len(df['Temp xPath links'])):    
    frame = WebDriverWait(driver, 30).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"iframe-form")))
    ref = WebDriverWait(driver, 30).until(EC.presence_of_element_located(By.xpath(df['Temp xPath links'][element])))
    driver.execute_script("arguments[0].scrollIntoView();", ref)
    driver.execute_script("arguments[0].click();", ref)

我正在努力进入下一页。任何帮助都会很好。

诱导
WebdriverWait
()并等待
元素可点击
()和
XPATH

WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//a[text()='151 Social']"))).click()
完整代码:

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

driver = webdriver.Chrome()
driver.get("https://www.halton.ca/For-Residents/Food-Safety/Dinewise/Search-Directory-of-Food-Premises-Dinewise")
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"iframe-form")))
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"btnOK"))).click()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"btnGetEstablishments"))).click()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//a[text()='151 Social']"))).click()

很好的回答,您还可以在末尾添加另一个步骤,以返回到所有餐厅的上一页,然后需要添加一些逻辑,以便在您点击页面上的最后一个结果后循环到下一页,返回到第一个窗口驱动程序。切换到.window(driver.window\u句柄[0])。还要添加一些time.sleep(),否则会出现请求错误。