How to extract Persian texts with Selenium in Python??
The name variable should be text, but it seems to be empty
from selenium import webdriver
from selenium.webdriver.common.by import By
website="https://www.digikala.com/product/dkp-3628808/%D8%B1%D9%88%D8%BA%D9%86-
%D8%B2%DB%8C%D8%AA%D9%88%D9%86-%D8%A8%DB%8C-%D8%A8%D9%88-
%DA%A9%D8%B1%DB%8C%D8%B3%D8%AA%D8%A7%D9%84-%D8%B7%D9%84%D8%A7%DB%8C%DB%8C-3000-
%D9%85%DB%8C%D9%84%DB%8C-%D9%84%DB%8C%D8%AA%D8%B1/"
driver = webdriver.Chrome(executable_path=r"C:\Users\Qazal\anaconda3\Lib\site-
packages\selenium\chromedriver.exe")
driver.get(website)
name=driver.find_element(By.XPATH,'//div[#class="mr-
4"]//a[#href="https://www.digikala.com/seller/AEVNX/"]')
name=name.text
name=name.encode("utf-8")
print(name)
The Xpath expression //*[#class="mr-4"]/div/a/p is producing the following output:
*emphasized text*from selenium import webdriver
from selenium.webdriver.chrome.service import Service
import time
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
webdriver_service = Service("./chromedriver") #Your chromedriver path
driver = webdriver.Chrome(service=webdriver_service, options=chrome_options)
url='https://www.digikala.com/product/dkp-3628808/%D8%B1%D9%88%D8%BA%D9%86-%20%20%D8%B2%DB%8C%D8%AA%D9%88%D9%86-%D8%A8%DB%8C-%D8%A8%D9%88-%20%20%DA%A9%D8%B1%DB%8C%D8%B3%D8%AA%D8%A7%D9%84-%D8%B7%D9%84%D8%A7%DB%8C%DB%8C-3000-%20%20%D9%85%DB%8C%D9%84%DB%8C-%D9%84%DB%8C%D8%AA%D8%B1/'
driver.get(url)
driver.maximize_window()
time.sleep(5)
for name in driver.find_elements(By.XPATH,'//*[#class="mr-4"]/div/a/p'):
print(name.text)
Output:
سراج احسان
رزاقی
Related
I'm starting to automate some processes and have problems with my code. I saw a tutorial of selenium but when i want to run the code, Visual Studio Code give me this Error:
ERROR: test_buscar (__main__.usando_unittest)
AttributeError: 'usando_unittest' object has no attribute 'driver'
This is my code
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
import time
class usando_unittest(unittest.TestCase):
def setup(self, driver):
self.driver = webdriver.Chrome(executable_path=r"C:PATH")
def test_buscar(self):
driver = self.driver
driver.get('http://www.google.com')
self.assertIn('Google', driver.title)
elemento = driver.find_element('id','input')
elemento.send_keys('Selenium')
elemento.send_keys(Keys.RETURN)
time.sleep(2)
assert'No se encontro el elemento' not in driver.page_source
def tearDown(self):
self.driver.close()
if __name__ == '__main__':
unittest.main()
For privacy, I didn't put the PATH in the code.
I need Help Please.
Thanks
Scenario:
open main page and click on "Accept All Cookies" (JSR223 Sampler1 in Once Only controller);
open pages from the set of parametrized urls (JSR223 Sampler2 in another controller).
JSR223 Sampler1 code for main page:
import org.apache.jmeter.samplers.SampleResult; import
org.openqa.selenium.chrome.ChromeOptions; import
org.openqa.selenium.chrome.ChromeDriver; import
org.openqa.selenium.WebDriver; import org.openqa.selenium.By; import
org.openqa.selenium.WebElement; import
org.openqa.selenium.support.ui.ExpectedConditions; import
org.openqa.selenium.support.ui.WebDriverWait; import
java.util.concurrent.TimeUnit;
System.setProperty("webdriver.chrome.driver",
"vars.get("webdriver_path")");
Map<String, Object> mobileEmulation = new HashMap<>();
mobileEmulation.put("userAgent", "vars.get("userAgent")");
Map<String, Object> chromeOptions = new HashMap<>();
chromeOptions.put("mobileEmulation", mobileEmulation); ChromeOptions
options = new ChromeOptions();
options.setExperimentalOption("mobileEmulation", mobileEmulation);
ChromeDriver driver = new ChromeDriver(options);
driver.get("https://vars.get("main_page")"); WebDriverWait wait = new
WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("xpath")));
driver.findElement(By.xpath("xpath")).click();
log.info(driver.getTitle());
JSR223 Sampler2 code for any page from the set of urls:
driver.get("https://${url}");
Error message:
Response message:javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: driver for class
Problem:
If I just copy all code from JSR223 Sampler1 to JSR223 Sampler2 and change destination url, urls are opening, but in unproper way - launching each time new browser instance, and I can't have realistic response time (for driver.get("url") only), because result provides time of Sampler work, which includes driver initialization, new browser instance start and it takes several seconds...
Could you please propose any ideas, how is possible to resolve this problem? To get all requests in 1 browser instance and to have realistic response time for all requests in JSR223 Sampler2 for browser.get("url") only?
Will appreciate for any help.
In the first JSR223 Sampler you need to store your driver instance into JMeter Variables like:
vars.putObject("driver", driver)
it should be the last line of your script
In the second JSR223 Sampler you need to get the driver instance from JMeter Variables like:
driver = vars.getObject("driver")
it should be the first line of your script
vars is the shorthand for JMeterVariables class instance, see the JavaDoc for all available functions and Top 8 JMeter Java Classes You Should Be Using with Groovy article for more information on JMeter API shorthands available for JSR223 Test Elements
P.S. the same approach with vars you should follow when executing driver.get() function like:
driver.get("https://" + vars.get("url"))
Is there a way to fire off the use of the chrome and gecko webdriver at the same time without duplicating code?
Right now I'm swapping between the two.
from selenium import webdriver
from selenium.common.exceptions import
def setUp(self):
# self.browser = webdriver.Firefox()
self.browser = webdriver.Chrome()
Yes, you can do it. However there must be some place where you specify the browser to be opened.
Browser name to be opened can be passed as an argument to setup method
from selenium import webdriver
def setUp(self, browserName):
if browserName == "Firefox":
self.browser = webdriver.Firefox()
elif browserName == "Chrome" :
self.browser = webdriver.Chrome()
Browser name to be opened can be read from some configuration/properties file.
from selenium import webdriver
def setUp(self):
browserName = #Code to read value from configuration file
if browserName == "Firefox":
self.browser = webdriver.Firefox()
elif browserName == "Chrome" :
self.browser = webdriver.Chrome()
You haven't specified the environment, so I'll go with this. This is how it could be done in Katalon Studio:
import org.openqa.selenium.WebDriver
import org.openqa.selenium.chrome.ChromeDriver
import com.kms.katalon.core.webui.driver.DriverFactory
System.setProperty("webdriver.chrome.driver", DriverFactory.getChromeDriverPath())
WebDriver driver1 = new ChromeDriver()
WebDriver driver2 = new FirefoxDriver()
DriverFactory.changeWebDriver(driver1)
// test with Chrome
DriverFactory.changeWebDriver(driver2)
// test with Firefox
Trying to run Selenium Webdriver script in Jmeter using JSR233 sampler. The script works fine in Eclipse IDE, however in Jmeter facing below error.
ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script JSR223 Sampler,
message: javax.script.ScriptException: In file: inline evaluation of:
``import java.util.HashMap; import org.openqa.selenium.WebDriver; import
org.openq . . . '' Encountered "," at line 28, column 25.
in inline evaluation of: ``import java.util.HashMap; import
org.openqa.selenium.WebDriver; import org.openq . . . '' at line number 28
javax.script.ScriptException: In file: inline evaluation of: ``import
java.util.HashMap; import org.openqa.selenium.WebDriver; import org.openq .
. . '' Encountered "," at line 28, column 25.
in inline evaluation of: ``import java.util.HashMap; import
org.openqa.selenium.WebDriver; import org.openq . . . '' at line number 28
at bsh.engine.BshScriptEngine.evalSource(BshScriptEngine.java:82) ~[bsh-
2.0b6.jar:2.0b6 2016-02-05 05:16:19]
at bsh.engine.BshScriptEngine.eval(BshScriptEngine.java:46) ~[bsh-
2.0b6.jar:2.0b6 2016-02-05 05:16:19]
at javax.script.AbstractScriptEngine.eval(Unknown Source) ~[?:1.8.0_181]
Below is the script trying to execute:
import java.util.HashMap;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium;
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
String downloadFilepath = "D:/MyDeskDownload";
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
// chromePrefs.put("profile.default_content_settings.popups", 0);
// chromePrefs.put("download.default_directory", downloadFilepath);
// chromePrefs.put("safebrowsing.enabled", "true");
ChromeOptions options1 = new ChromeOptions();
options1.setExperimentalOption("prefs", chromePrefs);
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options1);
WebDriver driver = new ChromeDriver(cap);
driver.setJavaScriptEnabled(true);
driver.get("http://google.com/");
I have gone through below references to get above script:
Running Selenium scripts with JMeter
How to use WDS variable in BSF or JSR233 (JMeter)
We could achieve launching a browser and performing actions with Selenium Webdriver config sampler with JavaScript however since we are unable to set capabilities with WDS, we are trying to achieve the same in JSR233.
From stacktrace it looks you are using JSR223 with Beanshell or Java (which will be beanshell).
Since it's Beanshell, it doesn't understands generics (diamond operator) so this line:
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
So you need to just switch the language to Groovy to fix the problem:
Beanshell doesn't support the diamond operator to if you really want to continue wiht Beanshell - change this line:
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
to this one
HashMap chromePrefs = new HashMap();
Be aware that starting from JMeter version 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting, the reasons are in:
Groovy supports all the modern Java language features
Groovy provides a lot of enhancements over standard Java SDK
Groovy provides much better performance than Beanshell
So consider migrating to Groovy, my expectation is that no changes will be required (you might need rewriting lambdas into closures if any, however the overhead will be minimal)
I want to load a chrome extension named scrapbook using Java. I searched a lot and tried different proposed methods to do so. However, none of those are working. Even it does not load the window maximized, let alone loading the extension. Here is the code:
package automationFramework;
import java.io.File;
import com.google.common.base.Function;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
public class FirstTestCase {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "F:\\SOFTWARE\\CHROME EXTENSIONS\\chromedriver_228.exe");
String pathToExtension = "C:\\Users\\user1\\AppData\\Local\\Google\Chrome\\User Data\\Profile 1\\oegnpmiddfljlloiklpkeelagaeejfai\\0.26.3_0\\";
ChromeOptions options = new ChromeOptions();
options.addArguments("-load-extension=" + pathToExtension);
options.addArguments("-open-maximized");
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.google.com");
}
}
I had the same issue, then I used the .crx file to add it on launch. I'm using python, but in Java it would be the same by using:
options.add_extension("idmgcext.crx");
the .crx file is in the same folder as the script or provide full path if in another folder you can get chrome extension for .crx file.