Cannot click an element , Selenium Python - python

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()

Related

Instagram Following Selenium Python

I am trying to figure out how to get Selenium to click the following section of an Instagram account, here is the html for it.
<a class=" _81NM2" href="/peacocktv/following/" tabindex="0"><span class="g47SY lOXF2">219</span> following</a>
When using instagram, the direct /username/following link does not actually bring up the following list, you would have to click the field manually on the website for it to appear.
log_in = WebDriverWait(driver, 8).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type = 'submit']"))).click()
not_now = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//button[contains(text(), "Not Now")]')))
not_now.click()
'''not_now2 = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//button[contains(text(), "Not Now")]')))
not_now2.click()'''
search_select = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[#placeholder = 'Search']")))
search_select.clear()
'''search_select.send_keys("russwest44", Keys.ENTER)
search_select.send_keys(Keys.ENTER)'''
# Clicks who the person is following:
following = driver.find_elements_by_xpath("//button[contains(text(),'following')]")
following.click()
Which is the best way to come up with the solution
Seems like couple of issues with your current code:
# Clicks who the person is following:
following = driver.find_elements_by_xpath("//button[contains(text(),'following')]")
following.click()
if you pay attention, you've shared HTML with a tag, so your xpath should be
//a[contains(text(),'following')]
not
//button[contains(text(),'following')]
also, you are using find_elements_by_xpath it would return a list of web elements. you can not directly trigger .click on it.
either switch to find_element or
following[0].click()

Selenium cant locate element inside ::before ::after

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)

How to click in a div?

I'm trying to perform a click on different div class TopBox. i tried the codes below but i don't get the click performed :
driver.find_element_by_css_selector('#home > div > div.row.topBoxs > div.col-xs-12.col-lg-10 > div > div:nth-child(1) > div').click()
and also :
driver.find_element_by_xpath('//*[#id="home"]/div/div[2]/div[1]/div/div[1]').click()
Below is the snapshot of the code of the box "mes posts" as exemple and the other boxes.
Try with xpath instead of css_selector.
driver.find_element_by_xpath('xpath_of_your_Div').click()
Here is what you have to do.
# get number of the toolboxes first
toolBoxes = len(driver.find_element_by_css_selector("div.boxs div[class^='topBox ']"))
# now you can click on each of them
for boxNumber in range(toolBoxes):
# you can use either xpath/css to get the nth box and click()
driver.find_element_by_xpath("(//div[#class='boxs']/div[starts-with(#class,'topBox ')])[" + str(boxNumber+1) + "]").click()
# waiting here so that you can see the element is clicked (optional)
time.sleep(2)
Firstly, check if you are able to locate the element. If it is not then following line should be throwing error:
WebElement theButton = driver.find_element_by_xpath('//div[#class='boxs']/div[1]/div');
alternatively pls try this xpath as well
'//div[#class='boxs']/div[1]/div/div/p/span'
Wait for the element till its clickable
wait = WebDriverWait(driver, 15)
wait.until(EC.element_to_be_clickable((By.XPATH, my_xpath)))
Then try clicking the element with .click()
Please let me know your observations which would help troubleshoot
Try this code with this css selector:
all_boxes = driver.find_elements_by_css_selector("div.content-box span")
for box_i in range(len(all_boxes)):
driver.find_elements_by_css_selector("div.content-box span")[box_i].click()
If you are getting an error, please let me know, what it is.

Finding an Xpath link on IMDB.com for the first drop down item in the search bar

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,

How to get element by attribute with Selenium, Xpath and Expected Condition

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]")))

Categories

Resources