Element to be located
I am trying to locate a span element inside a webpage, I have tried by XPath but its raise timeout error, I want to locate title span element inside Facebook marketplace product. url
here is my code :
def title_detector():
title = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, 'path'))).text
list_data = title.split("ISBN", 1)
Try this xpath //span[contains(text(),'isbn')]
You can't locate pseudo elements with XPath, only with CSS selector.
I see it's FaceBook with it's ugly class names...
I'm not sure this will work for you, maybe these class names are dynamic, but it worked for me this time.
Anyway, the css_locator for that span element is .dati1w0a.qt6c0cv9.hv4rvrfc.discj3wi .d2edcug0.hpfvmrgz.qv66sw1b.c1et5uql.lr9zc1uh.a8c37x1j.keod5gw0.nxhoafnm.aigsh9s9.qg6bub1s.fe6kdd0r.mau55g9w.c8b282yb.iv3no6db.o0t2es00.f530mmz5.hnhda86s.oo9gr5id
So, since we are trying to get it's before we can do it with the following JavaScript script:
span_locator = `.dati1w0a.qt6c0cv9.hv4rvrfc.discj3wi .d2edcug0.hpfvmrgz.qv66sw1b.c1et5uql.lr9zc1uh.a8c37x1j.keod5gw0.nxhoafnm.aigsh9s9.qg6bub1s.fe6kdd0r.mau55g9w.c8b282yb.iv3no6db.o0t2es00.f530mmz5.hnhda86s.oo9gr5id`
script = "return window.getComputedStyle(document.querySelector('{}'),':before').getPropertyValue('content')".format(span_locator)
print(driver.execute_script(script).strip())
In case the css selector above not working since the class names are dynamic there - try to locate that span with some stable css_locator, it is possible. Just have to try it several times until you see which class names are stable and which are not.
UPD:
You don't need to locate the pseudo elements there, will be enough to catch that span itself. So, it will be enough something like this:
span_locator = `.dati1w0a.qt6c0cv9.hv4rvrfc.discj3wi .d2edcug0.hpfvmrgz.qv66sw1b.c1et5uql.lr9zc1uh.a8c37x1j.keod5gw0.nxhoafnm.aigsh9s9.qg6bub1s.fe6kdd0r.mau55g9w.c8b282yb.iv3no6db.o0t2es00.f530mmz5.hnhda86s.oo9gr5id`
def title_detector():
title = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, 'span_locator'))).text
title = title.strip()
list_data = title.split("ISBN", 1)
Related
with selenium, i'm trying to click an element but not working with this element,
the page is here page (username/password :admin/admin)
wait2 = WebDriverWait(driver, 10)
element = wait2.until(EC.element_to_be_clickable((By.XPATH, '//*[#id="operate2a5a0448a8bf44a8898ec13e95b152fc"]/div/div[2]')))
element.click()
i tried this on other element in the same page and got no problem
no idea why not working on this element
operate2a5a0448a8bf44a8898ec13e95b152fc seems to be dynamically created id.
The simplest way to access this element is with text based XPath locator:
wait2.until(EC.element_to_be_clickable((By.XPATH, "//div[contains(text(),'Entry Registration')]"))).click()
you can try with below code as well :
//img[contains(#src,'registration')]/..
in code :
wait2 = WebDriverWait(driver, 10)
element = wait2.until(EC.element_to_be_clickable((By.XPATH, "//img[contains(#src,'registration')]/..")))
element.click()
I want to extract data in <div class="user-profile_list __relatives"> ... (see image)
Source code of the page https://gist.github.com/mascai/59e3bf779c2ba7cecb973ab9653ed419
My code
def get_relatives(driver):
relatives = []
relatives_container = driver.find_element_by_class_name("user-profile_list __relatives")
return relatives
driver = webdriver.Chrome(executable_path='chromedriver')
get_relatives(driver)
Error text
Message: no such element: Unable to locate element: {"method":"css selector","selector":".user-profile_list __relatives"}
This thing happens many time, its better to use xpath and search for class in it,
relatives_container = driver.find_element_by_xpath('//*[class="classuser-profile_list __relatives"]')
You can also try contains in xpath, it also work if there are multiple classes in that element and write only one of them
relatives_container = driver.find_element_by_xpath('//*[contains(#class, 'user-profile_list __relatives')]')
I can't seem to find the right Xpath for what I am trying to do:
the element I am trying to click on appears to be:
//*[#id="react-autowhatever-1--item-0"]/span/div[2]
but when run this code:
wait = WebDriverWait(imdbScrape.driver, 10)
wait.until(ec.element_to_be_clickable((By.XPATH,
'//*[#id="suggestion-search"]'))).send_keys("the"
+ "fifth element")
searchBar = wait.until(ec.element_to_be_clickable((By.XPATH,
'//*[#id="react-autowhatever-1--item-0"]/span/div[2]')))
searchBar.location_once_scrolled_into_view
urlToScrape = searchBar.get_attribute("href")
print(urlToScrape)
I get "None" as the result, I am assuming it because when I look on the page I don't see any "href" tags but I am wondering how I can get the first selection's link address
Any help would be appreciated
Thanks,
This is what I'm using:
getByAttribute = WebDriverWait(amazonDriver, 10).until(EC.visibility_of_element_located((By.XPATH, "//div[#an-attribute='data-category']")))
The element looks as follows:
<div class='nav-subnav' data-category='drugstore'>
This is present on every Amazon products page.
It times out and does not find the element.
Use #data-category to get element by attribute.
getByAttribute = WebDriverWait(amazonDriver, 10).until(EC.visibility_of_element_located((By.XPATH, "//div[#data-category]")))
CSS Selector:
getByAttribute = WebDriverWait(amazonDriver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div[data-category]")))
i'm trying to pick up links of youtube channels which are located as below:
<a id="author-text" class="yt-simple-endpoint style-scope ytd-comment-
renderer" href="/channel/UCUSy-h1fPG1L6X7KOe70asA"> <span class="style-
scope ytd-comment-renderer">Jörgen Nilsson</span></a>
So in the example above I would want to pick up "/channel/UCUSy-h1fPG1L6X7KOe70asA". So far i have tried many options but none work:
driver = webdriver.Chrome('C:/Users/me/Chrome Web Driver/chromedriver.exe')
api_url="https://www.youtube.com/watch?v=TQG7m1BFeRc"
driver.get(api_url)
time.sleep(2)
div = driver.find_element_by_class_name("yt-simple-endpoint style-scope ytd-comment-renderer")
but I get the following error:
InvalidSelectorException: Message: invalid selector: Compound class names not permitted
I also tried other approaches:
div = driver.find_elements_by_xpath("yt-simple-endpoint style-scope ytd-comment-renderer")
div = driver.find_element_by_class_name('yt-simple-endpoint style-scope ytd-comment-renderer')
div=driver.find_element_by_css_selector('.yt-simple-endpoint style-scope ytd-comment-renderer').get_attribute('href')
but no luck.. if someone could please help it would be much appreciated. Thank you
Your selectors are invalid:
driver.find_element_by_class_name("yt-simple-endpoint style-scope ytd-comment-renderer")
you cannot pass more than one class name to find_element_by_class_name method. You can try driver.find_element_by_class_name("ytd-comment-renderer")
driver.find_elements_by_xpath("yt-simple-endpoint style-scope ytd-comment-renderer")
it's not a correct XPath syntax. You probably mean driver.find_elements_by_xpath("//*[#class='yt-simple-endpoint style-scope ytd-comment-renderer']")
driver.find_element_by_css_selector('.yt-simple-endpoint style-scope ytd-comment-renderer')
each class name should start with the dot: driver.find_element_by_css_selector('.yt-simple-endpoint.style-scope.ytd-comment-renderer')
But the best way IMHO to identify by ID:
driver.find_element_by_id("author-text")
You can use BeautifulSoup in python to get the links in anchor tag having specific class names like soup.find_all('a', attrs={'class':'yt-simple-endpoint'}) you can read more here find_all using css