Problem clicking on link in header in selenium (python) - python

Hello I have an issue clicking on the link "Assortiment"
on this page: https://www.colruyt.be/fr
In order to click it I use :
element = browser.find_element_by_xpath('.//li[#class = "first leaf menu-mlid-9143"]')
element.click()

There are two elements that can be found with that xpath. The first one is hidden in the side pullout menu for the mobile version and the second is the one you want. Try scoping your xpath to the div with main-navigation class.
browser.find_element_by_xpath('.//div[#class = "main-navigation"]//li[#class = "first leaf menu-mlid-9143"]')

Related

Switch (internal) tab using Selenium

I've been trying to access elements of a website using Selenium but keep getting this message :
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
The problem I'm having seems to be that the website uses the same classes' name between two tabs, and just changes the visibility of one to display the other. For instance, when the first tab is displayed the first div's visibility is visible and the second is hidden. If the second tab is displayed, then the second div is visible, and the first hidden.
For the first tab, I have no problem accessing the desired elements. However, if the second tab is displayed, I get the above error message.
Is there a way to switch between to div like one would do between two iframe ?
This is what my code looks like for the moment :
driver.get(url) #get url
driver.find_element(By.XPATH, "//input[#id='ctl00_cphMaster_ConsultationCitoyenCriteresRecherche_CritereRechercheTabContainer_RechercheSimpleTabPanel_RechercheSimpleControl_MotsClesTextBox']").send_keys(string) #enter searched term
driver.find_element(By.XPATH, "//input[#id='ctl00_cphMaster_ConsultationCitoyenCriteresRecherche_CritereRechercheTabContainer_RechercheSimpleTabPanel_RechercheSimpleControl_PorteeRadioButtonList_1']").click()
driver.find_element(By.XPATH, "//input[#class='Bouton BoutonRechercher']").click() #click the search button
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH,"//div[#class='Header']")))
driver.find_element(By.XPATH, '//*[#id="__tab_ctl00_cphMaster_ConsultationCitoyenResultatsRecherche_ResultatsRechercheTabContainer_RechercheLETabPanel"]').click() #switch tab
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH,"//div[#class='Header']")))
while True:
listings = driver.find_elements_by_class_name("InscriptionSummaryValue")
c = 1
for i in listings:
page = int(driver.find_element(By.XPATH, "//span[#class='CurrentPage']").text) #can't find the element because it seems to be located in the first div

How can I select a specific element on a page when there is so many of that element? Selenium webdriver python;

Essentially what I am trying to do is select just the "Reply" box which circled in red, but there are many of these in the page overall. My aim is to be able to select the first "Reply" box on every page. How can I select just the first reply box for every post (with this link just being an example)?
Currently this doesn't seem to work:
reply = driver.find_element_by_xpath("//*[#id='content']/div/div[2]/div/div/div/div[1]/article/div/aside/ul/li[1]/div/div[2]/div/ul/li[7]/button/span/img")
reply.click()
Many thanks.
First way:
The XPath to locate any of that replay buttons is
//button[#title="Reply"]
So the XPath to locate the first replay button is
(//button[#title="Reply"])[1]
So you can simply
driver.find_element_by_xpath('(//button[#title="Reply"])[1]').click()
Second way:
With the XPath above you can retrieve a list of all the replay buttons and then get the first element in the list and click on it as following:
replay_buttons = driver.find_elements_by_xpath('//button[#title="Reply"]')
replay_buttons[0].click()
You can use css_selector instead of XPath here as well:
replay_buttons = driver.find_elements_by_css_selector('button[title="Reply"]')
replay_buttons[0].click()
Inspecting the page i saw that the class name of this button is: <button class="Button Button--link">
So you can use driver.find_elements_by_class_name('Button Button--link') ,
which returns a list of all the buttons.

How to find an Element by index in selenium Webdriver for python

This is HTML code of that page
From there I want to access the 2nd element by using class name "maxbutton-1" as it has 3 same buttons and I can't use xpath or any constant selector so want to use the indexing with class and can't find anything to do that in python particular.
Also tried the method used in java to do same thing but it didn't worked.
Link of that same page
just trying to automate the movie downloading process for any movie.
Thank you.
To click on first, second or third button, try to change number of element:
el1 = driver.find_element_by_xpath("(//a[#class='maxbutton-1 maxbutton maxbutton-download-links'])[1]")
el2 = driver.find_element_by_xpath("(//a[#class='maxbutton-1 maxbutton maxbutton-download-links'])[2]")
el3 = driver.find_element_by_xpath("(//a[#class='maxbutton-1 maxbutton maxbutton-download-links'])[3]")
then you can extract element href/link attribute like that:
link = el.get_attribute('href')
or click it like that:
el.click()

How to get next page in silenium?

I am working on selenium in python, I want to scrape all pages, but I am in trouble:
Here is the element I want to click:
I am using the folloing code:
link=driver.find_element_by_link_text ('2')
link.click()
But it give click on another element
Deos there exist another way to get next page?
First of all, sees like your element what you're trying to click overlapped by another one, so you need to wait for its becoming being clickable or other one disappear:
el = WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.XPATH,//div[#id="pagination_wrapper"]//li[#value="1"])))
or
WebDriverWait(driver, LONG_TIMEOUT
).until_not(EC.presence_of_element_located((By.XPATH,"//div[#class='close_cookie_alert']")))
Here's like you can find all of yours elements:
link1 = driver.find_element_by_xpath('//div[#id="pagination_wrapper"]//li[#value="1"]')
link2 = driver.find_element_by_xpath('//div[#id="pagination_wrapper"]//li[#class="2"]')
link3 = driver.find_element_by_xpath('//div[#id="pagination_wrapper"]//li[contains(text(),"text of the third element")]')
if usual click doesn't work, try to use click via javascript, like that:
driver.execute_script("arguments[0].click();", link1)
or, just move to the next page with:
driver.get('new_page')

finding button with similar href

a particular button (which allows me to jump to the second page) has a href
inputHref = /letsdeal?sectionLoadingID=m_timeline_loading_div_1485935999_0_36_timeline_unit%3A1%3A00000000001483240170%3A04611686018427387904%3A09223372036854775803%3A04611686018427387904&unit_cursor=timeline_unit%3A1%3A00000000001483240170%3A04611686018427387904%3A09223372036854775803%3A04611686018427387904&timeend=1485935999&timestart=0&tm=AQBwkKKSIKOhqAju&refid=17
and if i click on this button a second page open ups and a button (which takes me to the third page) has a href
inputHref = /letsdeal?sectionLoadingID=m_timeline_loading_div_1485935999_0_36_timeline_unit%3A1%3A00000000001482227114%3A04611686018427387904%3A09223372036854775798%3A04611686018427387904&unit_cursor=timeline_unit%3A1%3A00000000001482227114%3A04611686018427387904%3A09223372036854775798%3A04611686018427387904&timeend=1485935999&timestart=0&tm=AQBwkJZSIKOhqAju&refid=17
Both Href are different in the end part but similar in the start. How can i locate both of these buttons using the XPATH using one formula just like the following code.
extendButton = driver.wait.until(EC.presence_of_element_located(
(By.XPATH, "//a[contains(#href,'"+inputHref + "')]")))
You can apply a partial match using contains():
//a[contains(#href, "letsdeal")]
Or:
//a[contains(#href, "/letsdeal")]
Or, with a CSS selector:
driver.find_element_by_css_selector("a[href*=letsdeal]")
Note that I don't know how unique the "letsdeal" substring is on your page and whether it is used in other href attribute values.

Categories