1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Wrong output in the CSV file using xPath expression

Discussão em 'Python' iniciado por Stack, Setembro 17, 2025.

  1. Stack

    Stack Membro Participativo

    I wrote a code to get the following value "Exam Code", "Exam Name" and "Total Question". The issue is that in the put CSV file I am getting the wrong value in the "Exam Code" column. I am getting the same value as "Exam Name". The xPath looks fine to me. I don't know where is the issue happening. Following is the code:

    import pandas as pd
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.common.exceptions import NoSuchElementException
    import time

    option = Options()
    option.add_argument("--disable-infobars")
    option.add_argument("start-maximized")
    option.add_argument("--disable-extensions")
    option.add_experimental_option("excludeSwitches", ['enable-automation'])

    # Pass the argument 1 to allow and 2 to block
    # option.add_experimental_option("prefs", {
    # "profile.default_content_setting_values.notifications": 1
    # })
    driver = webdriver.Chrome(chrome_options=option, executable_path='C:\\Users\\Awais\\Desktop\\web crawling\\chromedriver.exe')

    url = ["https://www.marks4sure.com/210-060-exam.html",
    "https://www.marks4sure.com/210-065-exam.html",
    "https://www.marks4sure.com/200-355-exam.html",
    "https://www.marks4sure.com/9A0-127-exam.html",
    "https://www.marks4sure.com/300-470-exam.html",]

    driver.implicitly_wait(0.5)

    na = "N/A"

    # text = 'Note: This exam is available on Demand only. You can Pre-Order this Exam and we will arrange this for you.'
    links = []
    exam_code = []
    exam_name = []
    total_q = []

    for items in range(0, 5):
    driver.get(url[items])
    # if driver.find_element_by_xpath("//div[contains(@class, 'alert') and contains(@class, 'alert-danger')]") == text:
    # continue
    items += 1

    try:
    c_url = driver.current_url
    links.append(c_url)
    except:
    pass

    try:
    codes = driver.find_element_by_xpath('''//div[contains(@class, 'col-sm-6') and contains(@class, 'exam-row-data') and position() = 2]''')
    exam_code.append(codes.text)
    except:
    exam_code.append(na)

    try:
    names = driver.find_element_by_xpath('//*[@id="content"]/div/div[1]/div[2]/div[3]/div[2]/a')
    exam_name.append(names.text)
    except:
    exam_name.append(na)

    try:
    question = driver.find_element_by_xpath('//*[@id="content"]/div/div[1]/div[2]/div[4]/div[2]/strong')
    total_q.append(question.text)
    except:
    total_q.append(na)
    continue


    all_info = list(zip(links, exam_name, exam_name, total_q))
    print(all_info)

    df = pd.DataFrame(all_info, columns=["Links", "Exam Code", "Exam Name", "Total Question"])
    df.to_csv("data5.csv", index=False)
    driver.close()

    Continue reading...

Compartilhe esta Página