I have a problem with pressing a button by xpath using selenium - python

I want to press a button with the XPath of
//*[#id="rass-action-proceed"]
i don't know how to use selenium. can someone help me please?

This will allow you to do so using the respective XPath:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
elem = driver.find_element_by_xpath('//a[#id="rass-action-proceed"]')
elem.click() # Using WebElements 'click()' method for sheer simplicity

You can try this :
driver.find_element_by_id("rass-action-proceed").click()
or with Explicit wait :
WebDriverWait(driver , 10).until(EC.element_to_be_clickable((By.ID, "rass-action-proceed"))).click()
make sure to import :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Related

Creating script to search webpage [duplicate]

I was trying to access the search bar of this website: https://www.kissanime.ru
using selenium. I tried it using xpath, class, css_selector but every time this error pops up in the terminal.
selenium.common.exceptions.NoSuchElementException: Message: Unable to
locate element: //*[#id="keyword"]
My approach to the problem was :
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
driver=webdriver.Firefox()
driver.get("https://kissanime.ru/")
driver.maximize_window()
search=driver.find_element_by_xpath('//*[#id="keyword"]')
search.send_keys("boruto")
search.send_keys(Keys.RETURN)
Try adding some wait
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
search = WebDriverWait(driver, 10).until(expected_conditions.visibility_of_element_located((By.ID, "keyword")))
Add wait to avoid race condition
driver.implicitly_wait(20)

selenium scraper get empty list with Xpath

I am scraping price data on the website: https://fbx.freightos.com/ .
This is the code below:
from selenium import webdriver from selenium.webdriver.edge.service
import Service from selenium.webdriver.common.by import By
s = Service(e_driver_path)
driver = webdriver.Edge(service=s)
elements = driver.find_elements(By.XPATH,
/html/body/div[2]/div[1]/div[3]/div/div[1]/section/div[2]/div/div[2]/div/div[1]/div/div/div[1]/span)
content = "".join([element.text for element in elements])
print(content)
the problem is the result. It is an empty list.
As planned, it should be the "Current FBX" and the result looks like "$3,540".
Please help.
Thanks ahead.
Try to wait for required data
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
fbx = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//div[.='Current FBX']/following-sibling::span[normalize-space()]"))).text
Use webdriverwait() and wait for visibility of element located and following css selector
driver.get("https://fbx.freightos.com/")
print(WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.CSS_SELECTOR,".row.bottom-xs.between-xs div:nth-of-type(1)>div"))).text)
print(WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.CSS_SELECTOR,".row.bottom-xs.between-xs div:nth-of-type(1)>span"))).text)
You need to import below libraries.
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
This xpath will directly target the price web element.
Try fetching this after some wait as mentioned by #KunduK
//div[contains(text(),'Current FBX')]/parent::div/span
//span[#class='styled__Value-sc-1puuh0x-8 hIASbB']
You can also use xpath like this. simplicity is best

Trying to use WebDriverWait until presence_of_element_located instead of sleep.time in Selenim Python

I have this piece of code which works as it is:
time.sleep(0.7)
self.driver.find_element_by_xpath("anyXpath").click()
I make the program to wait for a time so the button on the pop-up window is really there to be clicked, and this solution works.
Now, I want to do the same but with WEbDriverWait and I wrote this:
WebDriverWait(self.driver,10).until(expected_conditions.presence_of_element_located(By.XPATH, "anyXpath"))
self.driver.find_element_by_xpath("anyXpath").click()
But the button is never clicked. What am I doing wrong? I am open to use another expected condition different from presence_of_element_located but I guess it should work with the latter also.
Thanks.
By importing WebDriverWait, expected_conditions, and By, you can wait until the WebDriver deems an element clickable before it clicks the element.
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.ui import WebDriverWait
WebDriverWait(10, self.driver).until(
expected_conditions.element_to_be_clickable(
(By.XPATH, "//xpath//to//element")
)
).click()
Make sure that you pass a tuple to element_to_be_clickable().
You can try this method. It wait for element to be click able
WebDriverWait(10, driver).until(EC.element_to_be_clickable(By.XPATH, "anyXpath"))
import
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

How to click the follow button on Instagram?

I am trying to click the follow button on Instagram using Python Selenium
https://www.instagram.com/luvly_zuby/?hl=en
I've tried the bellow code but it's not working.
#click follow
follow = driver.find_element_by_partial_link_text("Follo")
ActionChains(driver).move_to_element(follow).click().perform()
You can click on the element by using simple selenium click by finding the element using its text in the xpath and then using explicit wait on the element.
You can do it like:
follow_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//button[text()='Follow']")))
follow_button.click()
You need 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
This should work for you. Pass the WebElement to click(element) method.
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('testUrl')
follow = driver.find_element(By.XPATH, '//button[contains(text(),'Follow')]')
webdriver.ActionChains(driver).move_to_element(follow).click(follow).perform()
Try to find the element using this css selector a.BY3EC > button and wait using .element_to_be_clickable:
follow = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'a.BY3EC > button')))
follow.click()
Following import:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Selenium can't find element, but element is on the https://login.aliexpress.com/ webpage

On the website the selenium script cannot find the login and password fields. I tried to search by xpath, css selector, name and class name. But nothing worked.
from selenium import webdriver
from time import sleep
driver = webdriver.Firefox()
driver.get("https://login.aliexpress.com/")
driver.find_element_by_id("fm-login-id").send_keys("test_id")
driver.find_element_by_id("fm-login-password").clear()
driver.find_element_by_id("fm-login-password").send_keys("test_pass")
driver.find_element_by_id("fm-login-submit").click()`
I tried to do this with the help of Selenium IDE, and everything worked in the GUI. But after I exported the code to python and ran it, the program gave an error that it could not find the element.
The login form is inside of a frame, you need to switch to it first.
from selenium import webdriver
from time import sleep
driver = webdriver.Firefox()
driver.get("https://login.aliexpress.com/")
frame = driver.find_element_by_id("alibaba-login-box")
driver.switch_to.frame(frame)
driver.find_element_by_id("fm-login-id").send_keys("test_id")
driver.find_element_by_id("fm-login-password").clear()
driver.find_element_by_id("fm-login-password").send_keys("test_pass")
driver.find_element_by_id("fm-login-submit").click()
However as the the desired elements are within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired elements to be clickable.
You can use the following solution:
Using CSS_SELECTOR:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox(executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
driver.get("https://login.aliexpress.com/")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#alibaba-login-box[src^='https://passport.aliexpress.com/mini_login.htm?']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.fm-text#fm-login-id"))).send_keys("test_id")
driver.find_element_by_css_selector("input.fm-text#fm-login-password").send_keys("test_pass")
driver.find_element_by_css_selector("input.fm-button#fm-login-submit").click()
Interim Broswer Snapshot:
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
Reference
You can find a relevant discussion in
Ways to deal with #document under iframe

Categories