Home > database >  Trying to find the correct xpath
Trying to find the correct xpath

Time:12-20

I have made code, see following lines:

from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    import time
    from selenium.webdriver.common.by import By

    PATH = "C:\Program Files (x86)\chromedriver.exe"
    driver = webdriver.Chrome(PATH)

    driver.get("https://www.flashscore.com/match/jBvNMej6/#match-summary")
    print(driver.title)
    driver.maximize_window() # For maximizing window
    driver.implicitly_wait(10) # gives an implicit wait for 20 seconds

    driver.find_element_by_id('onetrust-reject-all-handler').click()
    time.sleep(2)
    driver.find_element(By.CLASS_NAME,'previewShowMore.showMore').click()

    main = driver.find_element(By.CLASS_NAME,'previewLine'[b[text()="Hot stat:"]]/text)
    print(main.text)

    time.sleep(2)
    driver.close()

However, I get the following error.

main = driver.find_element(By.CLASS_NAME,'previewLine'[b[text()="Hot stat:"]]/text) ^ SyntaxError: invalid syntax

What can I do to avoid this?

thx! : )

CodePudding user response:

Well, in this line

main = driver.find_element(By.CLASS_NAME,'previewLine'[b[text()="Hot stat:"]]/text)

You have made a great mix :)
Your locator is absolutely invalid.
Also, if you want to print the paragraph text without the "Hot streak" you will need to remove that string from the entire div (paragraph) text.
This should do what you are trying to achieve:

main = driver.find_element(By.XPATH,"//div[@class='previewLine' and ./b[text()='Hot streak']]").text
main = main.replace('Hot streak','')
print(main)

CodePudding user response:

I'm not finding any text 'Hot stat:'. You'll have to attach the html code where you found that.

I assume that you want to retrieve the text of a specific previewLine?

main = driver.find_element(By.XPATH ,'//div[@]/b[contains(text(),"Hot streak")]/..')
print(main.text)
  • Related