I've been working on this project where a site gives me 3 numbers portrayed with pictures.I quickly learned that the developers forgot to change the name of the PNG files in the HTML source so I learned to read those using code.
I have already setup a code with selenium and beatifulsoup to make a chrome page with that particular site.Give me time to login.Read the HTML Source in text code, find the numbers and instert them to the specified area and click the continue button.Then loop.
import time
import re
import numbers
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(executable_path = "C:/Users/user/Desktop/Personal/PythonScripts/chromedriver.exe")
driver.get('URL I USED')
time.sleep(20)
driver.refresh()
soup = BeautifulSoup(driver.page_source ,"lxml")
all_items = soup.find_all('img')
for item in all_items:
print('char:', item['src'][-5])
LIST = [item['src'][-5] for item in all_items]
new_list = []
for value in LIST:
try:
new_list.append(int(value))
except ValueError:
continue
for i in new_list:
print(i, end="")
try :
driver.find_element_by_tag_name('input').send_keys(new_list[0],new_list[1],new_list[2])
except :
print('Fail')
html_source = driver.page_source
print(html_source)
button = driver.find_element_by_xpath('//*[@id="main"]/form/div[3]/input')
button.click()
time.sleep(5)
while True:
for item in all_items:
print('char:', item['src'][-5])
LIST = [item['src'][-5] for item in all_items]
new_list = []
for value in LIST:
try:
new_list.append(int(value))
except ValueError:
continue
for i in new_list:
print(i, end="")
try :
driver.find_element_by_tag_name('input').send_keys(new_list[0],new_list[1],new_list[2])
except :
print('Fail')
html_source = driver.page_source
print(html_source)
new_list.clear()
button.click()
time.sleep(10)
driver.refresh()
I am fairly new to python and coding in general so feel free to point out my mistakes even unrelated to the topic.
My issue is the page doesn't necessarily reload after pressing continue and I tried not reloading it but my code "reads" the first survey correctly but fails on the proceeding ones as it fills the blank with the previously used numbers.(only the first one).I added more than necessary amount of sleep time.Removed the driver.refresh()
at the end of the loop.None of them worked.
Thanks in advance