質問編集履歴

1

文法の修正

2020/01/16 15:11

投稿

Zawakita
Zawakita

スコア7

test CHANGED
File without changes
test CHANGED
@@ -235,3 +235,173 @@
235
235
  ### 補足情報(FW/ツールのバージョンなど)
236
236
 
237
237
  Python 3.7
238
+
239
+
240
+
241
+ # 解決しました!
242
+
243
+ shiraiさんに教えていただいた方法を試したところ、問題なく動きました。
244
+
245
+ 本当にありがとうございました。
246
+
247
+
248
+
249
+ 修正したコードは以下の通りです。
250
+
251
+ ```Python
252
+
253
+ from selenium import webdriver
254
+
255
+ from selenium.webdriver.chrome.options import Options
256
+
257
+ from selenium.webdriver.support.ui import WebDriverWait
258
+
259
+ from selenium.webdriver.support import expected_conditions as EC
260
+
261
+ from selenium.webdriver.common.by import By
262
+
263
+ from bs4 import BeautifulSoup
264
+
265
+ import time
266
+
267
+ import lxml
268
+
269
+
270
+
271
+ op = Options()
272
+
273
+ op.add_argument("--disable-gpu");
274
+
275
+ op.add_argument("--disable-extensions");
276
+
277
+ op.add_argument("--proxy-server='direct://'");
278
+
279
+ op.add_argument("--proxy-bypass-list=*");
280
+
281
+ op.add_argument("--start-maximized");
282
+
283
+ # op.add_argument("--headless");
284
+
285
+ driver = webdriver.Chrome(options=op)
286
+
287
+
288
+
289
+ keys = ("ラーメン屋")
290
+
291
+ url = 'https://www.google.co.jp/maps/'
292
+
293
+ Selector = 'body'
294
+
295
+
296
+
297
+ driver.get(url)
298
+
299
+
300
+
301
+ WebDriverWait(driver, 30).until(
302
+
303
+ EC.presence_of_element_located((By.CSS_SELECTOR, Selector))
304
+
305
+ )
306
+
307
+
308
+
309
+ id = driver.find_element_by_id("searchboxinput")
310
+
311
+ id.send_keys(keys)
312
+
313
+ time.sleep(2)
314
+
315
+
316
+
317
+
318
+
319
+ Selector = "//*[@id='searchbox-searchbutton']"
320
+
321
+
322
+
323
+ search_button = driver.find_element_by_xpath(Selector)
324
+
325
+ search_button.click()
326
+
327
+
328
+
329
+ Selector_login = 'section-result'
330
+
331
+ WebDriverWait(driver, 30).until(
332
+
333
+ EC.presence_of_element_located((By.CLASS_NAME, Selector_login))
334
+
335
+ )
336
+
337
+ time.sleep(1)
338
+
339
+
340
+
341
+ for i in range(len(driver.find_elements_by_class_name(Selector_login))):
342
+
343
+ WebDriverWait(driver, 30).until(
344
+
345
+ EC.presence_of_element_located((By.CLASS_NAME, Selector_login))
346
+
347
+ )
348
+
349
+ login_button = driver.find_elements_by_class_name(Selector_login)[i]
350
+
351
+ login_button.click()
352
+
353
+
354
+
355
+ Selector = '//*[@id="pane"]/div/div[1]/div/div/div[2]/div[1]/div[1]/h1'
356
+
357
+ WebDriverWait(driver, 30).until(
358
+
359
+ EC.presence_of_element_located((By.XPATH, Selector))
360
+
361
+ )
362
+
363
+
364
+
365
+ page_source = driver.page_source
366
+
367
+ soup = BeautifulSoup(page_source, 'lxml')
368
+
369
+
370
+
371
+ title = soup.find("h1", {"class":"GLOBAL__gm2-headline-5 section-hero-header-title-title"}).text.strip()
372
+
373
+ links = soup.find_all(class_="section-info-text")
374
+
375
+
376
+
377
+ print(title, '\n')
378
+
379
+ for link in links:
380
+
381
+ print(link.text.strip())
382
+
383
+ print('---------------------', '\n')
384
+
385
+
386
+
387
+ Selector_back = '//*[@id="pane"]/div/div[1]/div/div/button'
388
+
389
+ WebDriverWait(driver, 30).until(
390
+
391
+ EC.presence_of_element_located((By.XPATH, Selector_back))
392
+
393
+ )
394
+
395
+ back_button = driver.find_element_by_xpath(Selector_back)
396
+
397
+ back_button.click()
398
+
399
+
400
+
401
+ WebDriverWait(driver, 30).until(
402
+
403
+ EC.presence_of_element_located((By.CLASS_NAME, Selector_login))
404
+
405
+ )
406
+
407
+ ```