Using python selenium i am trying to pull information from li class below is the code which i have tried but getting 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.
Please help!
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>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
There is no method called Attribute
in Selenium-Python. It is get_attribute
Also, you should put 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 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
CodePudding user response:
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.