Reputation: 17
Using Python Selenium, I am trying to pull information from the li class. Below is the code which I have tried, but I am getting an error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:
{"method":"xpath","selector":"//ul[@class='oas_columnsWrapper']//following::li[2]"}
I want to print: 218543-Not Applicable+OA
Below is the code which I have tried, but no luck.
How can I fix it?
print(browser.find_element(By.XPATH,"//ul[@class='oas_columnsWrapper']//following::li[2]").Attribute("innerHTML"))
My HTML code:
<ul class="omniAds__oas_columnsWrapper">
<li class="omniAds__oas_columns">1.</li>
<li class="omniAds__oas_columns">218543-Not Applicable+OA</li>
<li class="omniAds__oas_columns"></li>
<li class="omniAds__oas_columns">23-11-2021</li>
<li class="omniAds__oas_columns">19,452</li>
<li class="omniAds__oas_columns">546</li>
<li class="omniAds__oas_columns">3%</li>
<li class="omniAds__oas_columns undefined"><b>RUNNING</b></li>
<div class="omniAds__oas_columns"><a href="/my99acres/all_responses/OA"><b>16 Responses</b></a></div>
<li class="omniAds__oas_columns omniAds__oas_viewReportBtn" id="oas_viewReportBtn0">View Report</li>
</ul>
Upvotes: 1
Views: 1242
Reputation: 29382
There isn't any method called Attribute
in Selenium-Python. It is get_attribute
.
Also, you should add some delay
time.sleep(5)
second_li = browser.find_element(By.XPATH,"//ul[@class='omniAds__oas_columnsWrapper']//following::li[2]").get_attribute('innerText')
print(second_li)
Recommendation (use an explicit wait)
try:
second_li = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ul[@class='omniAds__oas_columnsWrapper']//following::li[2]"))).get_attribute('innerText')
print(second_li)
except:
print("Could not get the text")
pass
You will have to import
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Upvotes: 3
Reputation: 159
Your XPath selector is incorrect. It needs to be omniAds__oas_columnsWrapper
, not just oas_columnsWrapper
.
Also, you don't need to select the attribute of your WebElement to get its text. Selenium already has a builtin attribute for the WebElement, .text
to do this for you. However, if you still do want to use attributes, use .get_attribute()
instead of attribute().
Also, as a side note, I think your XPath can be simplified a bit.
Upvotes: 1