How to construct an XPath to extract the url www.fastreact.co.uk from the parent ul tag?

Multi tool use


How to construct an XPath to extract the url www.fastreact.co.uk from the parent ul tag?
Hi I want to navigate to this exact vendor url using selenium <li>www.fastreact.co.uk</li>
. But I'm stuck at this part.
<li>www.fastreact.co.uk</li>
vendor_url = browser.find_element_by_xpath('//ul[@class="check-list"]')
#vendor_url = browser.find_element_by_xpath('//ul[@class="check-list"][3]')
How do I navigate to that exact UL? From what I can see in the source code there are four occurrences where a UL contains the class="check-list ... "
.
Also once I manage to navigate to that node how do I output that selenium web element into human text?
class="check-list ... "
https://www.capterra.com/p/14890/Fastreact/
<h2 class="epsilon"><i class="ss-buildings icon-lead zeta"></i>VendorDetails</h2>
<ul class="check-list" >
<li>Fast React Systems</li>
<li>www.fastreact.co.uk</li>
<li>Founded 1999</li>
<li>United Kingdom</li>
</ul>
@Gabrielius Thanks for the tip. So I have four of these
<ul class="check-list ...
in the webpage. How do I select the 3rd UL from these four UL:s? capterra.com/p/14890/Fastreact– piggy
yesterday
<ul class="check-list ...
Try
//ul[@class="check-list"][3]/li[2]/text()
.– zx485
yesterday
//ul[@class="check-list"][3]/li[2]/text()
I thought you were trying to select third li element? Www.fastreact.co.uk? In either case you should use indexer [3] for ul like you did in the comment you posted. What was the problem you had with that?
– Gabrielius
yesterday
2 Answers
2
Here is a working example of what you are wanting:
NOTE: This was tested using Chrome build 67 with chromedriver 2.40
driver.get('https://www.capterra.com/p/14890/Fastreact/')
# this will go directly to the check-list item you are wanting
vendor_details = driver.find_element_by_xpath('.//ul[@class="check-list"][2]')
print(vendor_details.text)
# this will go directly to the link you are wanting
vendor_link= driver.find_element_by_xpath('.//ul[@class="check-list"][2]/li[2]')
print(vendor_link.text)
The print statements will display the text values of the elements in your console.
As per your question to extract the Vendor URL you need to induce WebDriverWait for the desired element to be visible and you can use the following solution:
Code Block:
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
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument('disable-infobars')
driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:UtilityBrowserDriverschromedriver.exe')
driver.get("https://www.capterra.com/p/14890/Fastreact/")
print(WebDriverWait(driver, 20).until(expected_conditions.visibility_of_element_located((By.XPATH, "//h2[@class='epsilon' and contains(.,'Vendor Details')]//following::ul[1]//following-sibling::li[2]"))).get_attribute("innerHTML"))
Console Output:
www.fastreact.co.uk
Why do you want OP to make all those imports and implement ExplicitWait to extract static data?
– Andersson
21 hours ago
@Andersson Good question, Long story cut short, it's nothing related to static data as such. It's all about new page where you are unsure when the Web Client achieves
'document.readyState' equal to "complete"
. Hence you must always induce a waiter. Let me know if you have further queries.– DebanjanB
21 hours ago
'document.readyState' equal to "complete"
To get required data you don't even need to wait for
readyState == "complete"
. State "interactive"
is more then enough, so I'm not sure about that "...must always induce a waiter..."– Andersson
21 hours ago
readyState == "complete"
"interactive"
@Andersson Yeap, perhaps you are correct on this individual instance/question. But my answer is pretty generic following the best practices. Of-coarse all the browsers doesn't wait for
readyState == "complete"
barring a few. After all, even achieving readyState == "complete"
(let's keep off "interactive"
) may not suffice enough for a user to interact with the prevailing DOM Tree. hence we need the waiter.– DebanjanB
20 hours ago
readyState == "complete"
readyState == "complete"
"interactive"
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Try this: //ul[@class="check-list"]/li[2]/text()
– Gabrielius
yesterday