when i work with this code it works fine, until the third index lists gets inputted.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import pandas as pd
import pandas
from openpyxl import load_workbook
from selenium.common.exceptions import NoSuchElementException
path = (r"C:\Users\hogyu\PycharmProjects\Project_Selenium\drivers\chromedriver.exe")
driver = webdriver.Chrome(path)
#B0041.2c, ZC518.3b
#cdis =["lec-3", "let-2","let-756","lgc-37","lgl-1","lim-8","lin-14","lin-2","lin-23","lin-29","lin-33","lin-41","lin-5","lin-8","lit-1","lmd-3","lmd-5","lmp-1","lon-8","lrp-1","lst-3","madd-2","madf-11","mai-1","math-41","mcm-10","mcm-6","mct-1","mdf-2","mdt-26","mdt-8","mec-2","men-1","mig-14","mix-1","mltn-12","mltn-6","mltn-9","mml-1","mms-19","mnk-1","moe-3"]
cdis = ["C53B4.4d","Y48A6B.11b","C50F4.10"]
#,"C50F4.10","ZK1098.10c","Y39G10AR.2","T03F1.1.1","C15B12.7a","F55A12.5","B0336.9a","F49E2.5a"]
book = load_workbook('test.xlsx')
writer = pandas.ExcelWriter('test.xlsx', engine='openpyxl')
writer.book = book
writer.sheets = {ws.title: ws for ws in book.worksheets}
df = pd.DataFrame(columns=['Gene_name', 'Description'])
dictionary = {'One': 1, 'Two': 2}
data = {}
for sheetname in writer.sheets:
df.to_excel(writer,sheet_name=sheetname, startrow=writer.sheets[sheetname].max_row, index = False,header= False)
for cdi in cdis:
driver.get("https://wormbase.org/#012-34-5")
search = driver.find_element_by_id("Search")
search.send_keys(cdi)
search.send_keys(Keys.RETURN)
ids = driver.find_elements_by_class_name("locus")
ids1 = driver.find_elements_by_class_name("gene-link")
ids2 = driver.find_elements_by_tag_name("h2")
if ids:
# try to search it again or do something else
id = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "locus")))
description = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "text-min")))
data['Gene_name'] = id.text
data['Description'] = description.text
df = df.append(data, ignore_index=True)
driver.back()
driver.refresh()
#df = df.append(data, ignore_index=True)
print(df)
time.sleep(1)
#elif ids1:
else:
id1 = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "gene-link")))
data['Gene_name'] = id1.text
description = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "text-min")))
data['Description'] = description.text
df = df.append(data, ignore_index=True)
driver.back()
driver.refresh()
#df = df.append(data, ignore_index=True)
print(df)
time.sleep(1)
but when i add an if, elif, else. the else is for the 3rd index. the code wont complete and stop at the first index as its searching for the else statement when it should be using the if statement.
for sheetname in writer.sheets:
df.to_excel(writer,sheet_name=sheetname, startrow=writer.sheets[sheetname].max_row, index = False,header= False)
for cdi in cdis:
driver.get("https://wormbase.org/#012-34-5")
search = driver.find_element_by_id("Search")
search.send_keys(cdi)
search.send_keys(Keys.RETURN)
ids = driver.find_elements_by_class_name("locus")
ids1 = driver.find_elements_by_class_name("gene-link")
ids2 = driver.find_elements_by_tag_name("h2")
if ids:
# try to search it again or do something else
id = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "locus")))
description = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "text-min")))
data['Gene_name'] = id.text
data['Description'] = description.text
df = df.append(data, ignore_index=True)
driver.back()
driver.refresh()
#df = df.append(data, ignore_index=True)
print(df)
time.sleep(1)
elif ids1:
id1 = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "gene-link")))
data['Gene_name'] = id1.text
description = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "text-min")))
data['Description'] = description.text
df = df.append(data, ignore_index=True)
driver.back()
driver.refresh()
#df = df.append(data, ignore_index=True)
print(df)
time.sleep(1)
else:
id2 = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.TAG_NAME, "h2")))
data['Gene_name'] = id2.text
description1 = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "description")))
data['Description'] = description1.text
driver.back()
driver.refresh()
df = df.append(data, ignore_index=True)
print(df)
time.sleep(1)
df.to_excel(r"C:/Users/hogyu/OneDrive/Desktop/Python codes/experiments/worbase_data.xlsx")
writer.save()
driver.quit()
the final product should be gene_name and description in the excel file 
I think the problem might be description1 = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "description"))) because it shows up as error. https://wormbase.org/species/c_elegans/gene/WBGene00008235#0-9f-10
<div class="evidence result">
description1 = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "evidence result")))
i am trying to find the evidence results but it seems to not work.
can anybody help with the if elif, and else, statements and the proper way to identify "evidence result" please
Aucun commentaire:
Enregistrer un commentaire