teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

3

typo

2018/03/19 22:51

投稿

umyu
umyu

スコア5846

answer CHANGED
@@ -13,7 +13,7 @@
13
13
 
14
14
 
15
15
  def condition(driver) -> bool:
16
- look_for = ['google', 'teratail', 'example']
16
+ look_for = ['google', 'treratail', 'example']
17
17
  url = driver.current_url
18
18
  if any(url.find(s) != -1 for s in look_for):
19
19
  print("match:{}".format(url))
@@ -30,10 +30,10 @@
30
30
  driver.get(url)
31
31
  try:
32
32
  WebDriverWait(driver, timeout=10).until(condition)
33
- "done:{}".format(driver.current_url)
33
+ print("done:{}".format(driver.current_url))
34
34
  except TimeoutException as ex:
35
35
  print("#" * 80)
36
- print(ex.stacktrace)
36
+ print(ex)
37
37
  finally:
38
38
  driver.quit()
39
39
 

2

refactoring

2018/03/19 22:51

投稿

umyu
umyu

スコア5846

answer CHANGED
@@ -3,7 +3,7 @@
3
3
  ```Python
4
4
  import random
5
5
  from selenium import webdriver
6
- from selenium.webdriver.common.by import By
6
+ from selenium.common.exceptions import TimeoutException
7
7
  from selenium.webdriver.support.ui import WebDriverWait
8
8
 
9
9
 
@@ -12,25 +12,28 @@
12
12
  return random.choice(URLS)
13
13
 
14
14
 
15
- def condition(driver):
15
+ def condition(driver) -> bool:
16
16
  look_for = ['google', 'teratail', 'example']
17
17
  url = driver.current_url
18
- for s in look_for:
19
- if url.find(s) != -1:
18
+ if any(url.find(s) != -1 for s in look_for):
20
- print("match:{}".format(s))
19
+ print("match:{}".format(url))
21
- return True
20
+ return True
22
21
 
23
22
  return False
24
23
 
25
24
 
26
- def main() ->None:
25
+ def main() -> None:
27
26
  driver = webdriver.Chrome()
28
27
  url = get_url()
29
28
  print("req:{}".format(url))
30
- driver.get(url)
31
29
  try:
30
+ driver.get(url)
31
+ try:
32
- WebDriverWait(driver, 10).until(condition)
32
+ WebDriverWait(driver, timeout=10).until(condition)
33
+ "done:{}".format(driver.current_url)
34
+ except TimeoutException as ex:
35
+ print("#" * 80)
33
- print(driver.current_url)
36
+ print(ex.stacktrace)
34
37
  finally:
35
38
  driver.quit()
36
39
 

1

conditionの判定を修正

2018/03/19 22:43

投稿

umyu
umyu

スコア5846

answer CHANGED
@@ -5,7 +5,6 @@
5
5
  from selenium import webdriver
6
6
  from selenium.webdriver.common.by import By
7
7
  from selenium.webdriver.support.ui import WebDriverWait
8
- from selenium.webdriver.support import expected_conditions as EC
9
8
 
10
9
 
11
10
  def get_url() -> str:
@@ -14,9 +13,11 @@
14
13
 
15
14
 
16
15
  def condition(driver):
17
- titles = ['google', 'teratail', 'example']
16
+ look_for = ['google', 'teratail', 'example']
17
+ url = driver.current_url
18
- for title in titles:
18
+ for s in look_for:
19
- if EC.title_contains(title):
19
+ if url.find(s) != -1:
20
+ print("match:{}".format(s))
20
21
  return True
21
22
 
22
23
  return False
@@ -24,7 +25,9 @@
24
25
 
25
26
  def main() ->None:
26
27
  driver = webdriver.Chrome()
28
+ url = get_url()
29
+ print("req:{}".format(url))
27
- driver.get(get_url())
30
+ driver.get(url)
28
31
  try:
29
32
  WebDriverWait(driver, 10).until(condition)
30
33
  print(driver.current_url)
@@ -34,6 +37,7 @@
34
37
 
35
38
  if __name__ == '__main__':
36
39
  main()
40
+
37
41
  ```
38
42
 
39
43
  ■参考情報