0

I want to check the innertext of a web element, but xpath does not find it even if i gave it the absolute path. I get no such element error on the line where i try to define Plaje

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Edge("D:\pariuri\python\MicrosoftWebDriver.exe")

driver.get("https://www.unibet.ro/betting#filter/all/all/all/all/in-play")

try:
    element_present = EC.presence_of_element_located((By.CLASS_NAME, 'KambiBC-event-result__score-list'))
    WebDriverWait(driver, 4).until(element_present)
except TimeoutException:
    print ('Timed out waiting for page to load') 

event = driver.find_elements_by_class_name('KambiBC-event-item KambiBC-event-item--type-match') 

for items in event:
   link = items.find_element_by_class_name('KambiBC-event-item__link')
   scoruri =  items.find_element_by_class_name('KambiBC-event-item__score-container') 

   scor1 =  scoruri.find_element_by_xpath(".//li[@class='KambiBC-event-result__match']/span[1]")
   scor2 =  scoruri.find_element_by_xpath(".//li[@class='KambiBC-event-result__match']/span[2]")

   print (scor1.text)
   print (scor2.text)
   if scor1.text == '0' and scor2.text == '0':

        link.click()
        Plaje = driver.find_element_by_xpath(".//*[@id='KambiBC-contentWrapper__bottom']/div/div/div/div/div[2]/div[2]/div[3]/div/div/div[4]/div[1]/div[4]/div[2]/div/div/ul/li[2]/ul/li[6]/div[1]/h3")
        print (Plaje.text)

enter image description here

Rius2
  • 347
  • 2
  • 6
  • 17

3 Answers3

1
  1. Always add some implicit wait after initiating the webdriver.

    driver = webdriver.Firefox()
    driver.implicitly_wait(10) # seconds
    
  2. Try with the below xpath.

    "//h3[contains(text(),'Total goluri']"
    

    or

    "//div[@class='KambiBC-bet-offer-subcategory__container']/h3[1]"
    

Hope this helps. Thanks.

EDIT: Its always advisable to use the implicit wait. We can handle the same using the explicit wait also. But we need to add the explicit wait for each and every element. Also there is a good change you might miss adding explicit wait to few elements and debug again. The choice is yours always.

santhosh kumar
  • 1,981
  • 1
  • 9
  • 28
  • Here i have identified the element using its inner text. If the objective is to find the innertext of that element, Can you share me the complete html DOM so that i can help you with some other xpath ways. – santhosh kumar Aug 30 '17 at 10:34
  • @ santhosh kumar to be honest i do not know how to give you the complete html DOM as it is to big to add as comment here. – Rius2 Aug 30 '17 at 10:41
  • 1
    Dude,, create a gist with the html code and share the gist link. – santhosh kumar Aug 30 '17 at 10:42
  • Interesting when i tried your answer i got the same error " no such element", but then i inserted the line you suggested `driver.implicitly_wait(10)` and it now works, but i read in the Selenium documentaion that the webdriver will wait for the page to load. Anyway glad it works now – Rius2 Aug 30 '17 at 11:03
  • Implicit wait doesn't wait for the page to load. It actually waits till the time for the availability of the web elements. – santhosh kumar Aug 30 '17 at 11:05
  • *Always add some implicit wait after initiating the webdriver* you shouldn't make such advices considering OP uses *Explicit wait*. I mean that word *Always* is inappropriate in this case – Andersson Aug 30 '17 at 11:07
  • @Andersson, If that is the case, one has to use explicit wait for each and every element in the complete script In all cases, its sensible to use some implicit wait by default. Considering, if some elements is going to take more time than the implicit wait, we can have a separate explicit wait for that element alone. Kindly feel free to correct me if i am wrong. Thanks. – santhosh kumar Aug 30 '17 at 11:16
  • @santhoshkumar, sooooo...? Check code in question and you'll see that OP uses Explicit wait. You should warn OP to not [mix Implicit and Explicit waits](https://stackoverflow.com/questions/29474296/clarification-on-the-cause-of-mixing-implicit-and-explicit-waits-of-selenium-doc) instead of "*Always add some implicit wait...*" – Andersson Aug 30 '17 at 11:23
  • @Anderson. Yes he used Explicit wait for few elements. And he missed for the webelement Plaje , which is throwing not found exception. – santhosh kumar Aug 30 '17 at 11:28
0

try using .get_attriute("value") or .get_attribute("innerHTML") instead of .text

Treehee
  • 117
  • 1
  • 2
  • 11
0

Try below :

//h3[contains(.,'Total goluri')]

hope it will help you :)

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125