[英]How to select element trough xpath in Selenium and Python?
I want to check the innertext of a web element, but xpath does not find it even if i gave it the absolute path. 我想检查Web元素的内部文本,但是即使我给了它绝对路径,xpath也找不到它。 I get no such element error on the line where i try to define
Plaje
我在尝试定义
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)
Always add some implicit wait after initiating the webdriver. 启动网络驱动程序后,请务必添加一些隐式等待。
driver = webdriver.Firefox() driver.implicitly_wait(10) # seconds
Try with the below xpath. 尝试使用下面的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.
选择永远是您的。
尝试使用.get_attriute("value")
或.get_attribute("innerHTML")
而不是.text
Try below : 请尝试以下方法:
//h3[contains(.,'Total goluri')]
hope it will help you :) 希望它能对您有所帮助:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.