Issue
I was using the below code snippet to extract the "Locations" link using selenium webdrive in python, but not able to extract the link, was only able to extract the text ("Locations"). Can anyone help me in this?
Link to extract from: https://www.thomasnet.com/company/siemens-corporation-10035100/profile?cov=NA&which=comp&what=Siemens+Corporation&cid=10035100&searchpos=1
Code Snippet used:
lnk_content = driver.find_element(By.XPATH,"//*[@id='__next']/div/div[2]/div/div[1]/div/div/button/span")
lnk = lnk_content.get_attribute("href")
print(lnk)
Solution
Agree with larks comment. See the below code to click on Location element and extract the URL which gets loaded.
Code:
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://www.thomasnet.com/company/siemens-corporation-10035100/profile?cov=NA&which=comp&what=Siemens+Corporation&cid=10035100&searchpos=1")
driver.maximize_window()
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, "(//span[text()='Locations'])[2]"))).click()
time.sleep(5)
location_url = driver.current_url
print(location_url)
Console:
https://www.thomasnet.com/company/siemens-corporation-10035100/branches?pg=1
Process finished with exit code 0
Answered By - Shawn
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.