How to locate the LOAD MORE button using Selenium Python - python

I'm having trouble trying to figure out how to locate the path for a "Load More Button" on this site I am trying to scrape.
https://www.ufc.com/athletes/all
The button html:
<a class="button" href="?gender=All&search=&page=1" title="Load more items"
rel="next">Load More</a>
"Load More" == $0
::after == 0$

To click on the element LOAD MORE you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
driver.get('https://www.ufc.com/athletes/all')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#onetrust-accept-btn-handler"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[title='Load more items']"))).click()
Using XPATH:
driver.get('https://www.ufc.com/athletes/all')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#id='onetrust-accept-btn-handler']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#title='Load more items']"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

You didn't provide any attempts and I can see 3 right off the top of my head that should work...
CSS selector, a[title='Load more items']
XPath, //a[text()='Load More']
By link text, "Load More"
In general you should prefer CSS selectors because they are more widely supported, faster, and have simpler syntax.

this is the XPATH for that button that you want
you can right-click on an element to inspect and right-click on it to copy XPath
driver.find_element(By.XPATH, ' //*[#id="block-
mainpagecontent"]/div/div/div[2]/div/div/ul/li/a').click()

Related

Unable to locate an element with the xpath expression using Selenium

I am now convinced that there is no solution to this. But in case there is someone out there who can help: Whole Element that I want to click on genesys cloud is (Its a part of a table):
<a target="_blank" data-bind="
attr: {
href: lastReportRun().reportUrl
},
lAttr: {
title: 'reports.list.grid.downloadColumn.fileTypes.' + lastReportFileType()
},
lString: 'reports.list.grid.downloadColumn.fileTypes.' + lastReportFileType()
" href="https://apps.usw2.pure.cloud/platform/api/v2/downloads/9161911a0307202a" title="XLSX">XLSX</a>
Element snapshot:
I am trying to locate this element to click on it and start downloading the .xlsx file. These are the things I have tried so far, but no luck. Can someone please help me understand where I can correct it.
Code trials:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "XLSX"))).click()
driver.find_element_by_link_text("XLSX").click()
driver.find_element_by_xpath(u'//a[text()="XLSX"]').click()
driver.find_element_by_xpath('//a[normalize-space(text())="XLSX').click()
driver.find_element(By.XPATH, "//input[#name='XLSX' and #value='XLSX']").click()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH, "//*input[#name='XLSX' and #value='XLSX']"))).click()
driver.find_element_by_xpath('/html/body/div[1]/div/div/div/div/div/div/div/div/div[2]/div[2]/div/div[5]/a').click()
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//*span[contains(., 'XLSX')]"))).click()
l=driver.find_element_by_xpath("//*a[#title='XLSX']")
l.click()
The desired element is a dynamic <a> element.
So to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using LINK_TEXT:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "XLSX"))).click()
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href^='https://apps.usw2'][title='XLSX'][data-bind]"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(#href, 'https://apps.usw2') and text()='XLSX'][#title='XLSX' and #data-bind]"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Trying to make selenium click on a button

I am trying to make selenium click the add to trolley button but there is an error the code which I am using is:
trolley = driver.find_element_by_xpath("//button[#role='button']")
trolley.click()
The inspect element of the button is:
<button class="Buttonstyles__Button-pv6mx8-2 SczzF" data-test="add-to-trolley-button-button" kind="primary" role="button" tabindex="0" type="button" xpath="1" style=""><span><span>Add<span class="sr-only"> </span> to trolley</span></span></button>
I was able to find a pythonspot article on clicking a button using selenium (https://pythonspot.com/selenium-click-button/) and they used the function:
trolley = driver.find_elements_by_xpath(`xpath here`)
trolley.click()
The difference between them is this one returns a list over a single element, im not sure why this might work over your method but this is what ive found. Just change that element to elements
To locate the element you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.find_element(By.CSS_SELECTOR, "button[class^='Buttonstyles__Button'][data-test='add-to-trolley-button-button'] > span > span").click()
Using XPATH:
driver.find_element(By.XPATH, "//button[starts-with(#class, 'Buttonstyles__Button') and #data-test='add-to-trolley-button-button']/span/span").click()
Ideally, to locate the clickable element you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[class^='Buttonstyles__Button'][data-test='add-to-trolley-button-button'] > span > span"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[starts-with(#class, 'Buttonstyles__Button') and #data-test='add-to-trolley-button-button']/span/span"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Clicking a span element in a class with python selenium

I am trying to click into the Over/Under Section on this Website:
https://www.oddsportal.com/soccer/chile/primera-division/curico-unido-o-higgins-CtsLggl6/
The HTML is:
<li class=" active" style="display: block;"><span class="topleft_corner"></span><span class="topright_corner"></span><strong><span>Over/Under</span></strong></li>
I have tried the following:
overunder=browser.find_element_by_link_text('Over/Under')
overunder=wait(browser, 5).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(), 'Over/Under']")))
overunder=browser.findElement(By.xpath("//span[contains(text(), 'Over/Under']"))
All of these followed by overunder.click()
However all result in a NosuchElementException.
How can I click this item?
I am trying to access and scrape the Over/Under Websites behind this section.
I copied the full xpath and this worked for me:
browser.find_element_by_xpath("/html/body/div[1]/div/div[2]/div[6]/div[1]/div/div[1]/div[2]/div[1]/div[5]/div[1]/ul/li[5]/a").click()
When I checked the page_source of the link you provided using selenium, this is what I get for area you're looking for:
<li class="" style="display: block;"><a onmousedown="uid(5)._onClick();return false;" title="Over/Under" href=""><span>O/U</span></a></li>
I used
browser.find_element_by_css_selector("a[title='Over/Under']").click()
which worked for me.
Instead of the element with text Over/Under, I see the element with text O/U
To click on the element with text as save you can use either of the following Locator Strategies:
Using css_selector:
driver.find_element(By.CSS_SELECTOR, "a[title='Over/Under'] > span").click()
Using xpath:
driver.find_element(By.XPATH, "//a[#title='Over/Under']/span[text()='O/U']").click()
Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[title='Over/Under'] > span"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#title='Over/Under']/span[text()='O/U']"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Browser Snapshot:

Python, Selenium: how to click on javascript:void(0) element

I have an "onclick" element in a webpage,
<a href="javascript:void(0)" onclick=
"toIndexHtml('3','http://xxxxxxxxxxx/trade','0')">
<i></i>
<span></span>
trade
</a>
It is shown in the webpage as a button and I want to click on it, I tried to search for it using the following code:
driver.find_element_by_xpath("//a[contains(#onclick,'toIndexHtml')]").click()
WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH,"/html/body/div/ul/li[3]/a"))).click()
Both are not working, please suggest if there is any other ways! Thanks in advance!
P.S.: I am using Chrome WebDriver and Chrome v64.
Your first locator looks perfect and should have worked.
Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[onclick^='toIndexHtml']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(#onclick,'toIndexHtml')]"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

How to click on a javascript/AJAX based element with onclick attribute using Selenium and Python

I'm trying to click a link in a dropdown menu in Selenium.
I'm accessing the element like so:
link = menu.find_element_by_xpath('//*[contains(text(), "Mark as shipped")]')
The link's href is javascript.void(0), and contains an onclick attribute which contains:
'com.ebay.app.myebay2.lineaction.service.LineActionAjax.processTransRequest("http://payments.ebay.com/ws/eBayISAPI.dll?OrderAction&transId=#TID#&action=4&pagetype=1883&ssPageName=STRK:MESO:SHP&itemid=_Item_Id", "_Item_Id", "987349587", "MarkShipped", "98739873", "_Item_Id_9874987_ss", 24")'
I've tried triggering this with:
click()
and
driver.execute_script(link.get_attribute('onclick'))
Also an ActionChain mousing over the link and clicking it.
But none seem to work. How do I trigger this?
The element is a AJAX element, so ideally to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using PARTIAL_LINK_TEXT:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Mark as shipped"))).click()
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[onclick*='MarkShipped']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(#onclick, 'MarkShipped') and contains(., 'Mark as shipped')]"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Categories