from selenium import webdriver from flask import Flask, render_template import random from pyvirtualdisplay import Display
app = Flask(__name__)
with Display():
options = webdriver.ChromeOptions()
options.add_argument('--headless')
driver = webdriver.Chrome(chrome_options=options)
driver.implicitly_wait(10)
t_file = '/home/parshuram/mysite/static/links'
target = open(t_file).read().splitlines()
try:
@app.route('/')
def get_jokes():
driver.get(random.choice(target))
jokes = driver.find_elements_by_tag_name('p')
mylist = []
for joke in jokes:
mylist.append(joke.text)
return render_template('joke.html', s=mylist)
if __name__ == "__main__":
app.run(debug=True)
except Exception as e:
print(e)
The same code works fine on my PC. However, here I only get the 1st element 'joke' displayed in 's' Here is the template
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sick Jokes</title>
<style>
body {
background-color:yellow;
}
div {
display: block;
overflow: auto;
border: 5px solid green;
padding: 45px;
margin: 15px;
text-align: center;
vertical-align: middle;
}
h2 {
font-size: 50px;
font-family: sans-serif;
color: black;
text-align: center
}
li {
font-family: Copperplate Gothic Light;
font-size: 22px;
color: black;
}
button {
position: relative;
color: black !important;
text-transform: uppercase;
font-family: Copperplate Gothic Light;
font-size: 18px;
background: #49a6cc;
padding: 20px;
border-radius: 50px;
display: inline-block;
border: none;
transition: all 0.4s ease 0s;
}
</style>
</head>
<body>
<header>
<h2>Sickest Jokes on the Internet!</h2></header>
<div>
<a href="{{ url_for('get_jokes') }}"><button>Press here for new Jokes if the page has no Jokes or you have read them already!</button></a>
</div>
{%for i in range(0, 21)%}
<div>
<li>{{s[i]}}</li>
</div>
{%endfor%}
</body>
</html>
Any ideas?