Clicking a span element in a class with python selenium - python

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:

Related

How to locate the LOAD MORE button using Selenium 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()

Web Scraping: How to click a specific href link in selenium python?

I wanted to automate a web page using webdriver(Python) wherein the script will click on the href link directly in order to navigate to other page. Couple of details in the specific tag:
<span class = "xyz", ui-sref ="abc", href= "/letters/letters_number/">
"hdf"
<i class = 'icons'>letters</i>
</span>
To click() on a the element with the href attribute 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, "span[href=/letters/letters_number/] i.icons"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[#href='/letters/letters_number/']//i[#class='icons']"))).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

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 get this element and click on it with selenium?

Trying to make my bot click on a submit button.
<div class="usertext-buttons">
<button type="submit" onclick="" class="save">save</button>
<button type="button" onclick="return cancel_usertext(this);" class="cancel" style="display:none">cancel</button>
<span class="status"></span></div>
I want to get the second row element with the type="submit"
driver.find_element_by_xpath doesn't work since the xpath is different for every post. What can I pull here that generally works?
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("button.save[type='submit'][onclick]").click()
Using xpath:
driver.find_element_by_xpath("//button[#class='save' and text()='save'][#type='submit' and #onclick]").click()
Ideally, to click on the element you have 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, "button.save[type='submit'][onclick]"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='save' and text()='save'][#type='submit' and #onclick]"))).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
Try using css selector:
driver.find_element_by_css_selector('div.usertext-buttons > button[type=submit]').click()

Categories