on python get url code is quite easy just four lines
URL = 'https://google.com'
chrome_options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
driver.get(URL)
so I thought it was easy golang too
I've searched many golang code reference but it quite large and complicate code to me
can you help me to get url with golang easily?
Don't need selenium for that
Here's the code
package main
import "github.com/fedesog/webdriver"
chromeDriver := webdriver.NewChromeDriver("./chromedriver") // Driver path
chromeDriver.Start()
desired := webdriver.Capabilities{"Platform": "MAC"}
required := webdriver.Capabilities{}
session, _ := chromeDriver.NewSession(desired, required)
session.Url("https://www.google.com")
hope it works to you
ciao!
Related
When starting the function
def run(driver_path):
driver = webdriver.Chrome(executable_path=driver_path)
driver.get('https://tproger.ru/quiz/real-programmer/')
button = driver.find_element_by_class_name("quiz_button")
button.click()
run(driver_path)
I'm getting errors like these:
<ipython-input-27-c5a7960e105f>:6: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
driver = webdriver.Chrome(executable_path=driver_path)
<ipython-input-27-c5a7960e105f>:10: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
button = driver.find_element_by_class_name("quiz_button")
... but I can't understand why.
I'm using WebDriver at the latest version for my Chrome's version. I don't why I get
find_element_by_* commands are deprecated
... when it's in the documentation that the command exists.
This error message...
DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
...implies that the find_element_by_* commands are deprecated in the latest Selenium Python libraries.
As AutomatedTester mentions: This DeprecationWarning was the reflection of the changes made with respect to the decision to simplify the APIs across the languages and this does that.
Solution
Instead you have to use find_element(). As an example:
You have to include the following imports
from selenium.webdriver.common.by import By
Using class_name:
button = driver.find_element_by_class_name("quiz_button")
Needs be replaced with:
button = driver.find_element(By.CLASS_NAME, "quiz_button")
Along the lines of, you also have to change the following:
Using id:
element = find_element_by_id("element_id")
Needs be replaced with:
element = driver.find_element(By.ID, "element_id")
Using name:
element = find_element_by_name("element_name")
Needs be replaced with:
element = driver.find_element(By.NAME, "element_name")
Using link_text:
element = find_element_by_link_text("element_link_text")
Needs be replaced with:
element = driver.find_element(By.LINK_TEXT, "element_link_text")
Using partial_link_text:
element = find_element_by_partial_link_text("element_partial_link_text")
Needs be replaced with:
element = driver.find_element(By.PARTIAL_LINK_TEXT, "element_partial_link_text")
Using tag_name:
element = find_element_by_tag_name("element_tag_name")
Needs be replaced with:
element = driver.find_element(By.TAG_NAME, "element_tag_name")
Using css_selector:
element = find_element_by_css_selector("element_css_selector")
Needs be replaced with:
element = driver.find_element(By.CSS_SELECTOR, "element_css_selector")
Using xpath:
element = find_element_by_xpath("element_xpath")
Needs be replaced with:
element = driver.find_element(By.XPATH, "element_xpath")
Note: If you are searching and replacing to implement the above changes, you will need to do the same thing for find_elements_*, i.e., the plural forms of find_element_*.
You may also find this upgrade guide useful as it covers some other unrelated changes you may need to make when upgrading: Upgrade to Selenium 4
#DebanjanB mentioned and explained the new structure. Also, it's better use these lines:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
s = Service('C:/Users/.../chromedriver.exe')
driver = webdriver.Chrome(service=s)
As others mentioned, you should use find_element() or find_elements() instead of find_element_by_*() or find_elements_by_*().
I wrote the regex pattern to replace the deprecated methods to new ones, so try this if you need.
# from - e.g. find_element_by_id("test")
find_element(s?)_by_([a-z]+)\((.*)
# to - e.g. find_element(By.ID, "test")
find_element$1(By.\U$2\E, $3
Note: you need the import line to use the new methods
from selenium.webdriver.common.by import By
Thank you #Stephen and undetected Selenium for your answers. After some time reading on how where to find an example of send_key, I found an amazing gist of examples.
The send_keys below example worked for me:
browser = webdriver.Chrome()
def test_key_down(driver):
driver.get('https://www.selenium.dev/selenium/web/single_text_input.html?#')
ActionChains(driver) \
.send_keys("abc") \
.perform()
test_key_down(browser)
Below is my code. It is running fine when I remove wait command from code but wnen I am adding wait in below code it started showing the error like :
javax.script.ScriptException: TypeError: null has no such function "visibilityOfElementLocated" in at line number 13
at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:470)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:454)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:406)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:402)
at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:155)
at javax.script.AbstractScriptEngine.eval(Unknown Source)
Code -
WDS.sampleResult.sampleStart()
var search1 = WDS.args[0]
var pkg=JavaImporter(org.openqa.selenium) //import java selenium packageimport java selenium package
var support_ui=JavaImporter(org.openqa.selenium.support.ui.WebDriverWait)import webdriver
var ui=JavaImporter(org.openqa.selenium.support.ui)
var wait=new support_ui.WebDriverWait(WDS.browser,java.time.Duration.ofSeconds(120))
var timeunit = java.util.concurrent.TimeUnit
WDS.browser.findElement(org.openqa.selenium.By.xpath("//input[#placeholder='Search']")).clear()
WDS.browser.findElement(org.openqa.selenium.By.xpath("//input[#placeholder='Search']")).sendKeys(search1)
WDS.browser.findElement(org.openqa.selenium.By.xpath("//td/img[#id='search']")).click()
wait.until(pkg.ExpectedConditions.visibilityOfElementLocated(org.openqa.selenium.By.xpath("//td[text()='224' and #class='SwsCount']")))This wait command is not working
WDS.sampleResult.sampleEnd()
I am expecting to use explicit wait in webdriver sampler. I am using apache-jmeter 5.5 version
Your code is not readable hence we're not able to provide a comprehensive answer.
This error:
null has no such function "visibilityOfElementLocated"
means that (most probably) your pgk.ExpectedConditions is null so I would recommend revisiting your code and checking where you're defining this pkg variable.
Example simple code showing how to use Explicit Waits in WebDriver Sampler with JavaScript:
WDS.sampleResult.sampleStart()
WDS.browser.get('https://duckduckgo.com')
var wait = new org.openqa.selenium.support.ui.WebDriverWait(WDS.browser, java.time.Duration.ofSeconds(5))
var by = org.openqa.selenium.By.id("search_form_input_homepage")
var condition = org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated(by)
var element = wait.until(condition)
element.sendKeys('jmeter')
element.submit()
WDS.sampleResult.sampleEnd()
Also be aware that your approach is not very future-proof as Nashorn engine has been removed from JDK 15 therefore you won't be able to use javascript language with newer versions of Java. It's better consider migrating to Groovy language, moreover it's the recommended scripting option since JMeter 3.1
when im downloading a page with selenium and process it with java jsoup. I get the hrefs in the source code like this:
Technical Trading
Is there a way to get the absolute url from this or to force selenium to transform it to an absolute url? Updating the links after getting the page doesn't sound like a clean solution.
If you get the href just with selenium, this works as expected:
yourElement.get_attribute('href')
This is a quick sample:
driver = webdriver.Chrome() # note this is my webdriver
driver.implicitly_wait(10)
url = "https://www.duckduckgo.co.uk"
driver.get(url)
aList = driver.find_elements(By.TAG_NAME, 'a')
for a in aList:
print(a.get_attribute('href'))
Output contains:
https://duckduckgo.com/spread
https://duckduckgo.com/spread
https://duckduckgo.com/app
https://duckduckgo.com/app
https://duckduckgo.com/newsletter
https://duckduckgo.com/newsletter
This is how the DOM looks: (it's relative - but gets the full path)
I'm trying to write this code using Selenium and Python:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://demo.guru99.com/test/login.html')
from selenium.webdriver.common.keys import Keys
email = firefox.find_element_by_xpath('//*[#id="email"]')
email.send_keys('user#gmail.com')
passwd = firefox.find_element_by_xpath('//*[#id="passwd"]')
passwd.send_keys('123456')
But I don't understand why this error occured, please help me
In your code, you are making a an instance of the webdriver.Firefox() object and calling it browser
browser = webdriver.Firefox()
Later on in your code, you try to find an element by xpath using a varible called firefox
email = firefox.find_element_by_xpath('//*[#id="email"]')
The problem is that that firefox was never actually created, I think what you meant to do is
email = browser.find_element_by_xpath('//*[#id="email"]')
You are defining the variable browser as a webdriver object. You then attempt to use firefox as a webdriver object, but you never defined the variable firefox. This is one way to solve your problem. You're better off calling the variable browser, rather than firefox, because then you can just change the browser type in your first line of code and the code will not be confusing.
browser = webdriver.Firefox()
browser.get('http://demo.guru99.com/test/login.html')
from selenium.webdriver.common.keys import Keys
email = browser.find_element_by_xpath('//*[#id="email"]')
email.send_keys('user#gmail.com')
passwd = browser.find_element_by_xpath('//*[#id="passwd"]')
passwd.send_keys('123456')
My program is having trouble getting an existing class from a webpage using Selenium. It seems that my WebDriver.execute_script function is not working.
import urllib
from selenium import webdriver
#Path to the chromedriver is definitely working fine.
path_to_chromedriver = 'C:\Users\Ben\Desktop\Coding\FreeFoodFinder\chromedriver.exe'
browser = webdriver.Chrome(executable_path = path_to_chromedriver)
url = 'http://www.maidservicetexas.com/'
browser.implicitly_wait(30)
browser.get(url)
content = browser.execute_script("document.getElementsByClassName('content')");
#Just printing the first character of the returned content's toString for now. Don't want the whole thing yet.
#Only ever prints 'N', the first letter of 'None'...so obviously it isn't finding the jsgenerated content even after waiting.
print content
My program returns 'None,' which tells me that the javascript function is not returning a value/being executed. Chrome's web dev tools tell me that 'content' is certainly a valid class name. The webpage isn't even dynamically generated (my eventual goal is to scrape dynamic content, which is why I make my WebDriver wait for 30 seconds before running the script.)
Return the value:
content = browser.execute_script("return document.getElementsByClassName('content');");