Webdriver Actionchains - click on the next page doesn't work - selenium-webdriver

I am new here and I tried to write a code that should go through all of die 3090 pages and extract the information given in the table (box-b).
But it doesn't work. It seems to stay on the first page and it extracts those informations over and over.
If any of you has an idea how I can fix it please help me :)
Lara
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
import time
url = "https://www.dieversicherer.de/versicherer/auto---reise/typklassenabfrage#orderBy=kh&orderDirection=ASC"
driver = webdriver.Chrome('/Users/laraschneider/Downloads/chromedriver')
driver.get(url)
time.sleep(5)
html = driver.page_source
soup = BeautifulSoup(html, "html.parser")
number = 0
while number < 3090:
results_html = soup.find_all('div', "ttw-entry__box-b")
results = list()
for result in results_html:
results.append(result.text)
for t in results:
print(t)
time.sleep(5)
element = driver.find_element_by_xpath("/html/body/main/div/div[5]/div/div/div/div[2]/div[2]/div[2]/div[2]/button[2]/div")
action = ActionChains(driver)
action.move_to_element(element)
action.click(element).perform()
number = number+1

Use driver.execute_script("arguments[0].click();", element) instead of ActionChains. Here is the full code:
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
url = "https://www.dieversicherer.de/versicherer/auto---reise/typklassenabfrage#orderBy=kh&orderDirection=ASC"
driver = webdriver.Chrome('/Users/laraschneider/Downloads/chromedriver')
driver.get(url)
time.sleep(5)
html = driver.page_source
soup = BeautifulSoup(html, "html.parser")
number = 0
while number < 3090:
results_html = soup.find_all('div', "ttw-entry__box-b")
results = list()
for result in results_html:
results.append(result.text)
for t in results:
print(t)
time.sleep(5)
element = driver.find_element_by_xpath('/html/body/main/div/div[5]/div/div/div/div[2]/div[2]/div[2]/div[2]/button[2]/div')
driver.execute_script("arguments[0].click();", element)
number = number+1

Related

How to make a click on button with selenium?

I'm writing a code with selenium.
My code should open YouTube, input a word, click on search button and open a video.
Everything except the last one. I can't open a video. Could you please help me?
My code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome('/Users/mariabiriulina/Desktop/chromedriver')
driver.get('https://youtube.com/')
searchbox = driver.find_element(By.XPATH, '//input[#id="search"]')
searchbox.click()
searchbox.send_keys('Justin Timberlake')
searchbutton = driver.find_element(By.XPATH, '//*[#id="search-icon-legacy"]')
searchbutton.click()
elements = driver.find_element(By.XPATH, '//*[#id="video-title"]/yt-formatted-string').click()
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome('/Users/mariabiriulina/Desktop/chromedriver')
wait = WebDriverWait(driver, 20)
driver.get('https://youtube.com/')
searchbox = driver.find_element(By.XPATH, '//input[#id="search"]')
searchbox.click()
searchbox.send_keys('Justin Timberlake')
searchbutton = driver.find_element(By.XPATH, '//*[#id="search-icon-legacy"]')
searchbutton.click()
video_titles_xpath = "//*[#id='video-title']/yt-formatted-string"
wait.until(EC.visibility_of_element_located((By.XPATH, video_titles_xpath)))
video_titles = driver.find_elements(By.XPATH, video_titles_xpath)
video_titles[0].click()

Trying to check if value has been updated in Selenium

I am trying to click the next page and then get the page number....It should come back 2 and I always get 1 so it doesnt look like it is waiting???
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
import requests
driver = webdriver.Chrome()
driver.get('https://cases.ra.kroll.com/claires/Home-DocketInfo')
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '(//*[#id="p-next-page"])[1]'))).click()
WebDriverWait(driver, 10).until(EC._element_if_visible((By.XPATH, '//*[#id="pagenum"]')))
print(driver.find_element(By.XPATH,'//*[#id="pagenum"]').get_attribute('value'))

Both katalon and eclipse facing the issue while uploading a file on automation. Mentioned in detail below

package tools
import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import java.awt.Robot
import java.awt.Toolkit
import java.awt.datatransfer.StringSelection
import java.awt.event.KeyEvent
import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
public class uploadFiles {
#Keyword
def uploadFile (TestObject to, String filePath) {
WebUI.click(to)
StringSelection ss = new StringSelection(filePath);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.delay(1000); //Millisecond 1 second delay only if needed
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.delay(1000); //Millisecond 1 second delay only if needed
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
}
}
Above code is placed in keywords folder in the project structure.
CustomKeywords.'tools.uploadFiles.uploadFile'(iWay_Product.findElement(By.cssSelector('input#fuDrivers')).click(),'D:\\UserImport_Template.xls')
Thread.sleep(2500) //Millisecond 2.5 second delay only if needed
//More files can be added here...
This above code has been used in the actual test cases. Always getting this below error.
org.openqa.selenium.InvalidArgumentException: invalid argument

Websocket Unit test : WS from ScalaTestRouteTest doesnt do a websocket request

I am trying to put in place a unit test for websocket. From the doc, I should be able to use WS
See below a sscce
package com.streamingout
import akka.http.scaladsl.model.ws.TextMessage
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.PathMatchers.Rest
import akka.http.scaladsl.testkit.{ScalatestRouteTest, WSProbe}
import akka.stream.scaladsl.{Flow, Sink, Source}
import org.scalatest.{FlatSpec, Matchers}
class Test extends FlatSpec with Matchers with ScalatestRouteTest{
//--------------- Flow ---------------
def flow = {
import scala.concurrent.duration._
val source = Source.tick(initialDelay = 0 second, interval = 1 second, tick = TextMessage("tick"))
Flow.fromSinkAndSource(Sink.ignore, source)
}
//-------------- Routing ------------
def route = {
path("/wskt") {
println("websocket ws")
handleWebSocketMessages(flow)
} ~
path(Rest) { pathRest =>
println("path Rest")
getFromFile(s"webapp/$pathRest")
}
}
// create a testing probe representing the client-side
val wsClient = WSProbe()
// WS creates a WebSocket request for testing
WS("/wskt", wsClient.flow) ~> route ~> check {
// check response for WS Upgrade headers
isWebSocketUpgrade shouldEqual true
}
}
When I run the test, I can see in my console the path Rest message, meaning that WS doesnt upgrade to Websocket.
Anyone knows what is wrong with my code?
I am using akka 2.4.7
Thank you
To make the above code work, in the route, the path /wkst should be without any leading slash
def route = {
path("wskt") {
println("websocket ws")
handleWebSocketMessages(flow)
} ~
path(Rest) { pathRest =>
println("path Rest")
getFromFile(s"webapp/$pathRest")
}
}

Cant configure my desktop application to Google Calendar API

I am trying to set up my project to use the Google Calendar API. So far I have downloaded the latest libraries and imported them. At the moment I am trying to follow the tutorial from the Google developers which is found here.
From what I found out according to this link draft10 has been deprecated and I am trying to use other classes
which do not belong to draft10.
The following are my current imports:
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.auth.oauth2.AuthorizationCodeTokenRequest;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeRequestUrl;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
import com.google.api.client.googleapis.batch.BatchRequest;
import com.google.api.client.googleapis.batch.json.JsonBatchCallback;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.googleapis.json.GoogleJsonError;
import com.google.api.client.http.HttpHeaders;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.DateTime;
import com.google.api.client.util.Lists;
import com.google.api.client.util.store.DataStoreFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.calendar.CalendarScopes;
import com.google.api.services.calendar.model.Calendar;
import com.google.api.services.calendar.model.CalendarList;
import com.google.api.services.calendar.model.CalendarListEntry;
import com.google.api.services.calendar.model.Event;
import com.google.api.services.calendar.model.EventDateTime;
import com.google.api.services.calendar.model.Events;
And the following is the method taken from the Google sample with some changes:
public void setUp() throws IOException {
httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
// The clientId and clientSecret can be found in Google Developers Console
String clientId = "YOUR_CLIENT_ID";
String clientSecret = "YOUR_CLIENT_SECRET";
// Or your redirect URL for web based applications.
String redirectUrl = "urn:ietf:wg:oauth:2.0:oob";
ArrayList<String> scopes = new ArrayList<String>();
scopes.add("https://www.googleapis.com/auth/calendar");
// Step 1: Authorize -->
String authorizationUrl = new GoogleAuthorizationCodeRequestUrl(clientId, redirectUrl, scopes)
.build();
// Point or redirect your user to the authorizationUrl.
System.out.println("Go to the following link in your browser:");
System.out.println(authorizationUrl);
// Read the authorization code from the standard input stream.
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("What is the authorization code?");
String code = in.readLine();
// End of Step 1 <--
// Step 2: Exchange -->
GoogleTokenResponse response = new GoogleAuthorizationCodeTokenRequest(httpTransport, jsonFactory,
clientId, clientSecret, code, redirectUrl).execute();
// End of Step 2 <--
GoogleAccessProtectedResource accessProtectedResource = new GoogleAccessProtectedResource(
response.accessToken, httpTransport, jsonFactory, clientId, clientSecret,
response.refreshToken);
Calendar service = new Calendar(httpTransport, accessProtectedResource, jsonFactory);
service.setApplicationName("YOUR_APPLICATION_NAME");
}
The only problem is with the GoogleAccessProtectedResource class. It is giving me the following error: GoogleAccessProtectedResource cannot be resolved to a type.
Does anyone have any ideas on how I can get around this?
I managed to figure this out. All I had to do was to import the following packages:
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.services.plus.Plus;
import com.google.api.services.plus.PlusScopes;
And replace the following code:
GoogleAccessProtectedResource accessProtectedResource = new GoogleAccessProtectedResource(
response.accessToken, httpTransport, jsonFactory, clientId, clientSecret,
response.refreshToken);
Calendar service = new Calendar(httpTransport, accessProtectedResource, jsonFactory);
service.setApplicationName("YOUR_APPLICATION_NAME");
With this code:
GoogleCredential credential;
credential = new GoogleCredential.Builder().setTransport(httpTransport)
.setJsonFactory(jsonFactory).setServiceAccountId("[[INSERT SERVICE ACCOUNT EMAIL HERE]]")
.setServiceAccountScopes(Collections.singleton(PlusScopes.PLUS_ME))
.setServiceAccountPrivateKeyFromP12File(new File("key.p12"))
.build();
Plus plus = new Plus.Builder(httpTransport, jsonFactory, credential)
.setApplicationName("YOUR_APPLICATION_NAME")
.build();

Resources