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
Related
I am trying to click this button with Selenium:
<button class="MuiButtonBase-root MuiButton-root jss38 MuiButton-contained MuiButton-containedPrimary" tabindex="0" type="button" data-test="unifiedCrazyButton"><span class="MuiButton-label">Let's get crazy</span><span class="MuiTouchRipple-root"></span></button>
I can't do it by the class name, because there are other buttons with the same class name, and also the xpath value in this format:
/html/body/div[10]/div[3]/div/div[3]/button[2]
keeps changing which is unfortunate.
The only identifying factor seems to be
data-test="unifiedCrazyButton"
How can I click this button with Selenium?
The desired element is a dynamic element, so to click on the clickable 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, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[data-test='unifiedCrazyButton'] span.MuiButton-label"))).click()
Using XPATH:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[#data-test='unifiedCrazyButton']//span[#class='MuiButton-label' and contains(., 'get crazy')]"))).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
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:
Can't seem to click on a 'close' button to close a pup up window that I have opened before to scrape the data that it displays.
The button has the following html script:
<div class="closebutton" onclick="return hs.close(this)" title="Schließen"></div>
I was trying the following:
driver.find_element_by_xpath("//div[#class='closebutton']").click()
But I get the error:
Message: element not interactable
Anyone have a clue how to make the element interactable?
To click on the element you can use either of the following Locator Strategies:
Using css_selector:
driver.find_element_by_css_selector("button.closebutton[title='Schließen']").click()
Using xpath:
driver.find_element_by_xpath("//div[#class='closebutton' and #title='Schließen']").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, "button.closebutton[title='Schließen']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='closebutton' and #title='Schließen']"))).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
I am using Python 3 and Selenium(Chromedriver). I want to check for a element with this command.
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[3]/div/div[6]/div[2]/div[2]/div/form/div[2]/div[5]/div/div/div/div[2]/button"))).click()
The problem is that the XPATH is constantly changing between two paths:
/html/body/div[3]/div/div[6]/div[2]/div[2]/div/form/div[2]/div[5]/div/div/div/div[2]/button
/html/body/div[3]/div/div[3]/div[2]/div[2]/div/form/div[2]/div[5]/div/div/div/div[2]/button
I want to tell Python, that if the element is not found, it should search for the other XPATH.
If you know a method to find the element without the XPATH, i would also be happy with the solution.
It does not work if you search for the elemnt by its containing text, because the language from the sites changes if use a proxy.
This is the "Inspect Element" code of the button:
<button aria-label="Mobilnummer hinzufügen" class="bg-white css-1eajgu7 ex41m6f0 btn-secondary-dark " type="button">Hinzufügen</button>
To click on the element with text as Hinzufügen you can use either of the following Locator Strategies:
Using css_selector:
driver.find_element_by_css_selector("button.bg-white.btn-secondary-dark[aria-label='Mobilnummer hinzufügen']").click()
Using xpath:
driver.find_element_by_xpath("//button[#aria-label='Mobilnummer hinzufügen' and text()='Hinzufügen']").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, "button.bg-white.btn-secondary-dark[aria-label='Mobilnummer hinzufügen']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#aria-label='Mobilnummer hinzufügen' and text()='Hinzufügen']))).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 to use this XPath:
//div[contains(#class, 'mex-mobile-phone')]//button[contains(#class, 'btn-secondary-dark')]
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()