-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwithSelenium.py
60 lines (43 loc) · 1.6 KB
/
withSelenium.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import selenium
from selenium import webdriver
import pyttsx3
import sys
from bs4 import BeautifulSoup
import time
import pyttsx3
url = sys.argv[1]
driver = webdriver.Chrome("./chromedriver")
driver.get(url)
body_text = driver.find_element_by_tag_name('body').text
def highlight(element, effect_time, color, border):
"""Highlights (blinks) a Selenium Webdriver element"""
xelem = element._parent
def apply_style(s):
xelem.execute_script("arguments[0].setAttribute('style', arguments[1]);",
element, s)
original_style = element.get_attribute('style')
apply_style("border: {0}px solid {1};".format(border, color))
time.sleep(effect_time)
apply_style(original_style)
print(body_text)
soup = BeautifulSoup(driver.page_source,"html.parser")
sentences = body_text.split(".")
for s in sentences:
try:
my_elm = driver.find_elements_by_xpath(f"""//*[contains(text(),"{s}")]""")
xelem = my_elm[0]._parent
def apply_style(s):
xelem.execute_script("arguments[0].setAttribute('style', arguments[1]);",
my_elm[0], s)
original_style = my_elm[0].get_attribute('style')
apply_style("border: {0}px solid {1};".format("2", "blue"))
#time.sleep(effect_time)
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[37].id)
engine.say(s)
engine.runAndWait()
apply_style(original_style)
print(s)
except Exception as e:
print(e,s)