So I've tried almost every method that I knew of including xpath and nothing. It should be a simple find element and click and that's it, but I can't figure it out.
Here is my code
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
ff_options = Options()
#profile
binary = FirefoxBinary("C:\\Program Files\\Mozilla Firefox\\firefox.exe")
profile = webdriver.FirefoxProfile('C:\\Users\\bravoj\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\7k4o5psw.CCC Deafualt')
ff_driver = webdriver.Firefox(executable_path='C:\\Users\\bravoj\Downloads\\geckodriver.exe')
#fire fox driver
ff_driver.get('about:profiles')
#ff_driver.get('about:preferences#home')
ff_driver.find_element_by_id('profiles-set-as-default').click()
There is a duplicate code too
'profiles-set-as-default' is not id attribute, it's data-l10n-id attribute. You can use css_selector to locate it, and get unique element using previous brother element with data-l10n-args {CCC Deafult}
ff_driver.find_element_by_css_selector('[data-l10n-args*="CCC Deafult"] ~ [data-l10n-id="profiles-set-as-default"]')
The element with text as Set as default profile is a dynamic element so to locate and 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, "div.tab#profiles button[data-l10n-id='profiles-set-as-default']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#data-l10n-id='profiles-set-as-default' and text()='Set as default profile']"))).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 have problems with something I don't even know the name.
I'm trying to reach the link next to where it says src= "LINK" with selenium.
I have class name = tWeCI, I guess I need to achieve using this but I have no idea how to do it.
Code trials:
from selenium import webdriver
from selenium.webdriver.common.by import By
browser = webdriver.Chrome()
browser.get("https://www.instagram.com/p/CYMHMEGBSRT/")
a = browser.find_element(by=By.CLASS_NAME, value="tWeCl")
Output:
https://instagram.fist4-1.fna.fbcdn.net/v/t50.2886-16/271301182_1082110899295465_6686845989868801609_n.mp4?efg=eyJ2ZW5jb2RlX3RhZyI6InZ0c192b2RfdXJsZ2VuLjcyMC5jbGlwcy5iYXNlbGluZSIsInFlX2dyb3VwcyI6IltcImlnX3dlYl9kZWxpdmVyeV92dHNfb3RmXCJdIn0&_nc_ht=instagram.fist4-1.fna.fbcdn.net&_nc_cat=101&_nc_ohc=6RJahYZ39DIAX_ul9E4&edm=AABBvjUBAAAA&vs=453007466354843_3354420889&_nc_vs=HBksFQAYJEdENjZLeERwbE1LVExOZ0RBRWtPNE5YLWNzeGNicV9FQUFBRhUAAsgBABUAGCRHS0ZaS1JCMldIUXZxRzhCQUJ4bFB2ZnVrbE1hYnFfRUFBQUYVAgLIAQAoABgAGwAVAAAmqN%2FFrpms4z8VAigCQzMsF0A%2BmZmZmZmaGBJkYXNoX2Jhc2VsaW5lXzFfdjERAHX%2BBwA%3D&_nc_rid=c8bb85b1e6&ccb=7-4&oe=62408107&oh=00_AT-E77LrFH7G6VVXGeh8bRFQsu95hhQlqUGZdinzFBIUYQ&_nc_sid=83d603"
Snapshot of the HTML:
To print the value of the src attribute you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get('https://www.instagram.com/p/CYMHMEGBSRT/')
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "video.tWeCl"))).get_attribute("src"))
Using XPATH:
driver.get('https://www.instagram.com/p/CYMHMEGBSRT/')
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//video[#class='tWeCl']"))).get_attribute("src"))
Console Output:
https://instagram.fpnq13-2.fna.fbcdn.net/v/t50.2886-16/271301182_1082110899295465_6686845989868801609_n.mp4?efg=eyJ2ZW5jb2RlX3RhZyI6InZ0c192b2RfdXJsZ2VuLjcyMC5jbGlwcy5iYXNlbGluZSIsInFlX2dyb3VwcyI6IltcImlnX3dlYl9kZWxpdmVyeV92dHNfb3RmXCJdIn0&_nc_ht=instagram.fpnq13-2.fna.fbcdn.net&_nc_cat=101&_nc_ohc=6RJahYZ39DIAX-Iphu9&edm=AABBvjUBAAAA&vs=453007466354843_3354420889&_nc_vs=HBksFQAYJEdENjZLeERwbE1LVExOZ0RBRWtPNE5YLWNzeGNicV9FQUFBRhUAAsgBABUAGCRHS0ZaS1JCMldIUXZxRzhCQUJ4bFB2ZnVrbE1hYnFfRUFBQUYVAgLIAQAoABgAGwAVAAAmqN%2FFrpms4z8VAigCQzMsF0A%2BmZmZmZmaGBJkYXNoX2Jhc2VsaW5lXzFfdjERAHX%2BBwA%3D&_nc_rid=34a08d5a01&ccb=7-4&oe=62408107&oh=00_AT9sh_BH__zjeReDB7lde4t3avzYqDjimTJRnoZi6Lj-TQ&_nc_sid=83d603
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
On twitch.tv/esl_csgo I want to click on the channel points button/icon but it keeps giving me the Error
Message: no such element: Unable to locate element
I have searched and tried various methods of finding the element for over 4 hours and I have not found a way to click on the element
This is my code but it can not click on the button I want to, help would really be appreciated.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import time
options = Options()
options.add_argument("--user-data-dir=C:\\Users\\Me\\Desktop\\UserData")
options.page_load_strategy = 'normal'
driver = webdriver.Chrome(options=options)
driver.get("https://twitch.tv/esl_csgo")
time.sleep(10)
element = driver.find_element_by_xpath('//*[#id="c7037441c8fd58e7e0ac6326babcf03d"]/div/div[1]/div/div/div/div/div/section/div/div[5]/div[2]/div[2]/div[1]/div/div/div/div[1]/div[2]/button/div/div/div/div[2]/span')
element.click()
To click on the first channel points button/icon 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, "div[class^='InjectLayout-sc'] > div span[data-test-selector]"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[starts-with(#class, 'InjectLayout-sc')]/div//span[#data-test-selector]"))).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 want to click on "new order" icon in mt4 web terminal using selenium module in python
This is the code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome('./chromedriver')
driver.get("https://www.mql5.com/en/trading")
new_order = driver.find_element_by_xpath('/html/body/div[3]/div[1]/a[1]/span[1]')
new_order.click()
And this is the error that I get:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[3]/div[1]/a[1]/span[1]"}
(Session info: chrome=86.0.4240.198)
What is the correct way to locate that button, I searched and found some ways to locate elements for selenium but I couldn't get any of them work for me.
Looks like your page is dealing with iframes. So while the above answer has good practices, you also need to switch to the iframe:
driver.switch_to.iframe(self,frame reference)
Look for more details at https://www.techbeamers.com/switch-between-iframes-selenium-python/ or https://stackoverflow.com/a/24286392/1387701
The element with tooltip as New Order is 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 element to be clickable.
You can use the following xpath based Locator Strategies:
driver.get('https://www.mql5.com/en/trading')
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='webTerminalHost']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='Connect to an Account']//following-sibling::div[1]/span"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#title='New Order']/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
You can use a different xpath:
new_order = driver.find_element_by_xpath('//a[#title="New Order"]')
But I would suggest By, WebDriverWait, and expected_conditions:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome('./chromedriver')
driver.get("https://www.mql5.com/en/trading")
time.sleep(5)
iframe = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//iframe[#id="webTerminalHost"]')))
driver.switch_to.frame(iframe)
new_order = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//a[#title="New Order"]')))
new_order.click()
I have two problems: first, I cannot click on the show all bottom; and, second, I cannot get the data from the high chart.
I saw some examples for the high chart on Stack Overflow; however, I did not get how people figure our which JS code to execute.
I tried the following code to achieve that:
from selenium import webdriver
DRIVER_PATH = r"C:\Users\XX\Downloads\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(executable_path=DRIVER_PATH)
url = "https://siterankdata.com/wsj.com"
driver.get(url)
driver.find_element_by_xpath('//*[#id="smallchart"]/div/div/svg/g[17]/g/text/tspan').click() # Does not work I try to click on the show all button.
I would appreciate any help!
To click on show all button Use WebDriverWait() and wait for element_to_be_clickable() and following xpath
driver.get(url)
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//div[#id='smallchart']//*[name()='svg']/*[name()='g'][17]/*[name()='g']/*[name()='text']/*[name()='tspan']"))).click()
You need to import below libraries
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
The element with text as Show all is a svg element so 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:
driver.get("https://siterankdata.com/wsj.com")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#smallchart svg g text[text-anchor='start'] tspan"))).click()
Using XPATH:
driver.get("https://siterankdata.com/wsj.com")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#id="smallchart"]//*[name()='svg']//*[name()='g']//*[name()='text']//*[name()='tspan' and text()='Show all']"))).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
References
You can find a couple of relevant discussions on interacting with SVG element in:
How to access to 'rect' type element through Selenium-Python
Clicking on svg using selenium python
I am making a script that will navigate through a website and perform some actions for me. But I am stuck trying to make my script click on the following elemnt:
<img src="../Images/copy.gif">
As there is no ID or class name for this element, i tried to find the element by xpath and clicking on it by using the following code:
import selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import time
....
copy = driver.find_element_by_xpath('//[#id="divGrid"]/div[2]/div[2]/table/tbody/tr[1]/td[1]/div/span/a[2]')
copy.click()
This isn't working, so I am open to suggestions to how to solve this issue.
The desired element is a JavaScript enabled element so 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, "a[title='Copy Trip']>img[src$='/Images/copy.gif']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#title='Copy Trip' and contains(#href, 'OnCopy')]/img[contains(#src, '/Images/copy.gif')]"))).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