Python Selenium move to element - python

driver = webdriver.Firefox()
driver.maximize_window()
url = r"https://www.nba.com/stats/players/traditional"
driver.get(url)
advanced = driver.find_element(By.XPATH, r'//div[2]/div[2]/div[3]/section[1]/div/nav/div[3]/ul/li[2]/a')
action = ActionChains(driver)
action.move_to_element(advanced)
advanced.click()
return error is always: element could not be scrolled into view.
I've tried other versions of this code including:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//div[2]/div[2]/div[3]/section[1]/div/nav/div[3]/ul/li[2]/a'))).click()
Please help, Thank you already

You need to accept the cookie consent button and then click on of toggle to expand the dropdown and then select the element.
driver.get("https://www.nba.com/stats/players/traditional")
#accept cookie consent
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"button#onetrust-accept-btn-handler"))).click()
time.sleep(1)
#expand
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"(//div[#class='StatsQuickNavSelector_nav__JzoME']/button)[last()]"))).click()
#click on specific item
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//a[text()='Advanced']"))).click()

Try using:
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//div[2]/div[2]/div[3]/section[1]/div/nav/div[3]/ul/li[2]/a')))
driver.execute_script("arguments[0].scrollIntoView();", element)
element.click()
To scroll the element into view.
resource

Related

Selenium python hidden element cant be clicked unless hovered over

I want to create a program that will automatically host a krunker map when i run it but to host it the program has to click a button which only shows up if u hover over the map and i dont know how to do that with selenium (ps im gonna set the server to private and i dont think i can just do that with a link and i dont wanna use any code that moves the mouse like pyautogui. If there is a better way to host a pivate custom map (with password) please share.
driver = uc.Chrome()
driver.get('https://krunker.io')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#id='onetrust-accept-btn-handler']"))).click()
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//div[#id='menuBtnHost' and contains(., 'Host Game')]"))).click()
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//div[#id='menuWindow' and contains(., 'Custom')]"))).click()
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//div[#id='hostCMapPickr']"))).click()
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='bigMenTab' and contains(., 'search')]"))).click()
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//div[#id='mapList']"))).click()
mapname = driver.find_element(By.ID,"mpSrch")
mapname.send_keys('Zombie_Bulwark')
mapname.send_keys(Keys.ENTER);
<<<what must i do here to click the button?
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='mapActionB']"))).click() <<<button i wanna click
Update
There is a way to simulate the mousehover in selenium
You can try the following
import undetected_chromedriver as uc # pip install undetected-chromedriver
from selenium.webdriver.common.action_chains import ActionChains
driver = uc.Chrome()
mapp = driver.find_element(By.XPATH, 'put the map xpath here')
mousehover = ActionChains(driver)
mousehover.move_to_element(mapp)
mousehover.perform()
# your mouse click
# WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='mapActionB']"))).click()

Clicking to the Login button using selenium

in a web scraping project I wanted to collect some data from https://www.businesstimes.com.sg and wanted to login into this. I couldn't figure out how to click the login button (check the image below) using Selenium.
I tried with the CSS_Selector, Class_Name, By.ID methods too, apart from this XPATH method to select the button, but I didn't get success.
Here is my code,
def login_in(login_url):
options = webdriver.ChromeOptions()
lists = ['disable-popup-blocking']
caps = DesiredCapabilities().CHROME
caps["pageLoadStrategy"] = "normal"
options.add_argument("--window-size=1920,1080")
options.add_argument("--disable-extensions")
options.add_argument("--disable-notifications")
options.add_argument("--disable-Advertisement")
options.add_argument("--disable-popup-blocking")
username = 'insert_username'
password = 'insert_password'
driver = webdriver.Chrome(executable_path= r"E:\chromedriver\chromedriver.exe", options=options) #add your chrome path
driver.get(login_url)
button = driver.find_element(By.XPATH, '//*[#id="sph_login"]')
driver.execute_script("arguments[0].click();", button)
time.sleep(3)
driver.find_element(By.ID, "IDToken1").send_keys(username) # input user name
time.sleep(5)
driver.find_element(By.ID, "IDToken2").send_keys(password) # input password
time.sleep(2)
loginbutton = driver.find_element(By.ID, "btnLogin")
driver.execute_script("arguments[0].click();", loginbutton)
return driver
login_in('https://www.businesstimes.com.sg/')
Please help me with this. Thank you!
you do not need to click the button. The button has an onclick attribut so you can just do this:
driver.execute_script("_mySPHObj.openLogin()")
and the login popup will open.
There are more than one matches for your XPath, that's why the link is not getting clicked, try the below XPath:
driver.find_element(By.XPATH, ".//*[#class='user-signup-section w-100 d-block']//a[text()='LOG IN']").click()

Scroll to a button with Selenium Python

I'm working on a webscraping script for a school project. I have to scroll down to a button to click, but I can't make it work.
I tried the following code:
driver.get('https://www.goodreads.com/book/show/28187.The_Lightning_Thief');
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[3]/div/div[1]/div/div/button')))
button.click()
time.sleep(5)
element = driver.find_element(By.CLASS_NAME,"Button Button--transparent Button--small")
actions = ActionChains(driver)
actions.move_to_element(element).perform()
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '''//*[#id="ReviewsSection"]/div[5]/div/div[4]/a''')))
button.click()
I recieved the following error:
Message: no such element: Unable to locate element: {"method":"css selector","selector":".Button Button--transparent Button--small"}
I also tried this, but it didn't work either:
driver.execute_script("arguments[0].scrollIntoView();", element)
When you scroll down on this page it will load in more reviews, so I guess that the button in the bottom I want to click don't load in, so I also tried to scroll down a few times and then try to scroll to the element, but it didn't work.
Can someone please help out?
EDITED:
I also tried this code:
driver = webdriver.Chrome()
driver.get('https://www.goodreads.com/book/show/13335037-divergent');
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[3]/div/div[1]/div/div/button')))
button.click()
time.sleep(3)
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(3)
element = driver.find_element(By.CSS_SELECTOR, "div.Divider.Divider--contents.Divider--largeMargin")
driver.execute_script("arguments[0].scrollIntoView();", element)
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '''//*[#id="ReviewsSection"]/div[5]/div/div[4]/a''')))
button.click()
This will scroll down to the bottom once end let the remaining reviews load in, then get the element and scroll again. But it's not scrolling down enough so the See all reviews and ratings won't be in view and can't be clicked.
Your selector is invalid since you're trying to pass multiple class names to search by class. You need to pass single #class only or to use another locator type, e.g.
element = driver.find_element(By.CSS_SELECTOR, ".Button.Button--transparent.Button--small")
P.S. Note that there are 4 elements with the same set of class names ("Button Button--transparent Button--small"), so search by class names is not a good option in this case
element = driver.find_element(By.XPATH, '//a[.="See all reviews and ratings"]')

Why would commands in selenium works seperately but if put in script selenium does'nt locate the elements

The problem i am facing is when i run the whole script it throws error of element not clickable or not found. While when i run it command per command it works.
If anyone can explain the reason and why it behaves this way i ll be very grateful.
codeExample:
driver.find_element(By.XPATH, "//div[#id=\'Content_C164_Col00\']/div/div/div[2]/div/div/div/div/div/button/span/span/span[3]").click()
driver.find_elements(By.CLASS_NAME, "fxs_c_datepicker_button")[1].click()
driver.find_element(By.CLASS_NAME, "fxs_btn.fxs_btn_cta.fxs_fRight").click()
ERROR:
ElementClickInterceptedException: Message: element click intercepted: Element ... is not clickable at point (238, 772). Other element would receive the click: ...
(Session info: chrome=101.0.4951.64)
Stacktrace:
Backtrace:
I am working in VS code as my editor.
I reproduced your problem and had the same error. What i did to fix it is just scroll to the element before clicking it.
Try this out
driver.find_element(By.XPATH, "//div[#id=\'Content_C164_Col00\']/div/div/div[2]/div/div/div/div/div/button/span/span/span[3]").click()
driver.find_elements(By.CLASS_NAME, "fxs_c_datepicker_button")[1].click()
actions = ActionChains(driver)
button = driver.find_element(By.CLASS_NAME, "fxs_btn.fxs_btn_cta.fxs_fRight")
actions.move_to_element(button)
button.click()
I also noticed that after a few seconds a popup appears on the website. Make sure you click that one away as it may intercept the click on the apply button.
Update:
Here's the full code from opening the website to selecting date and clicking the apply button.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
# Opening browser and maximizing it
driver.get("https://www.fxstreet.com/economic-calendar")
driver.maximize_window()
# Click 'Continue to site'
driver.find_element(By.CLASS_NAME, "fxs_prestitial-continue").click()
# Wait until popup appears and cancel it
driver.implicitly_wait(10)
driver.find_element(By.XPATH, "//button[#id='onesignal-slidedown-cancel-button' and text()='Cancel']").click()
# Click the datepicker button and choose date
driver.find_element(By.XPATH, "//div[#id=\'Content_C164_Col00\']/div/div/div[2]/div/div/div/div/div/button/span/span/span[3]").click()
driver.find_elements(By.CLASS_NAME, "fxs_c_datepicker_button")[1].click()
# Creating actions instance
actions = ActionChains(driver)
# Scrolling to 'Apply' button and clicking it
button = driver.find_element(By.CLASS_NAME, "fxs_btn.fxs_btn_cta.fxs_fRight")
actions.move_to_element(button)
button.click()
I tested a new version that works:
WebDriverWait(driver, 120).until(EC.element_to_be_clickable((By.ID, "onesignal-slidedown-cancel-button"))).click()
while True:
try:
studio = driver.find_element(By.CLASS_NAME, "fxs_headline_tiny").text
# print(studio)
driver.execute_script("window.scrollTo(0, 450)")
driver.find_element(By.CLASS_NAME, "fxs_icon.fa-calendar-alt.fa-w-14").click()
time.sleep(0.5)
exact_date = driver.find_elements(By.CLASS_NAME, "fxs_c_datepicker_button")
for i in range(len(exact_date)):
exact_date_i = exact_date[i].text
if(exact_date_i == "Today"):
time.sleep(2)
exact_date[i].click()
break
break
except:
print("Studio not found")
driver.find_element(By.CLASS_NAME, "fxs_btn.fxs_btn_cta.fxs_fRight").click()

Can't click the button to view more results, getting exception element not clickable

I am trying to scrape some data from this site https://easy.co.il/list/Shopping, when you scroll down the list of businesses it has a button at the end to view more results, when I try to click it using .click function it raises an exception that element is not intractable, I have also tried using Keys.ENTER still same exception, I have tried waiting for the element to be clickable using this code.
results = driver.find_elements_by_xpath('//div[#class="biz-item "]')
print(len(results))
button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//button[#id="nextPageButton"]')))
the len(results) prints 25 which is all the businesses visible till the view more results button.
I have also tried locating the button, the element is visible on the page but just not clickable.
Can someone please look in to this? Thankyou!
You need to scroll the element before click.Use location_once_scrolled_into_view to scroll the element.
driver.get("https://easy.co.il/list/Shopping")
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//button[#id="nextPageButton"]')))
button.location_once_scrolled_into_view
button.click()
If you still gets the error then induce java scripts executor to click.
driver.get("https://easy.co.il/list/Shopping")
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//button[#id="nextPageButton"]')))
button.location_once_scrolled_into_view
driver.execute_script("arguments[0].click();", button)
Use .visibility_of_element_located method, then scroll:
button = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, '//button[#id="nextPageButton"]')))
button.location_once_scrolled_into_view
button.click()

Categories