mo_mny
mo_mny

Reputation: 11

is it possible to use a stem part of multiple xpaths to create a program to randomly selects one?(selenium)

Is it possible to use a stem part of multiple xpaths to create a program to randomly selects one?

Say for example we are given these multiple xpaths:

//a[@href='/colour/red/yellow']

//a[@href='/colour/red/blue']

//a[@href='/colour/blue/ornage']

//a[@href='/colour/yellow/green']

Is it possible to randomly select one of the xpaths by using part of the xpath in a code, e.g:

option1 = browser.find_element_by_xpath("part of xpath")

I know this line of code wont work, but is there a specific function to use instead of browser.find_element_by_xpath

I am a beginner at programming pls help.

[EDIT]: I have found this function instead:

find_element_by_partial_link_text('')

I'm not sure how this function works, but would i be able to use this instead on links of each example, e.g:

I want a code that randomly picks between both parts of site, could i use something similar to:

option1 = find_element_by_partial_link_text('http://www.colour.com/colour/red')

Is this even possible?

[UPDATE]: Breaks Software's solution worked; it has allowed me to use part of link, e.g. http://www.colour.com/colour/red

The program its self would then make a decision between:

http://www.colour.com/colour/red/yellow

and http://www.colour.com/colour/red/blue

Code:

#open website
browser = webdriver.Chrome()
browser.get(('http://www.colour.com/colour'))
from random import randint

#generating program to select random link
target_links = browser.find_elements_by_css_selector("a[href^='/colour/red")

random_index = randint(0, len(target_links) - 1)
target_links[random_index].click()

Thank you for everyone's help, I hope this question can help others facing similar problem.

Upvotes: 0

Views: 72

Answers (4)

Breaks Software
Breaks Software

Reputation: 1761

I think that you are asking to find all of the links that start with a particular URL, then randomly choose one of them to follow.

target_links = browser.find_elements_by_css_selector("a[href^='http://www.colour.com']")

random_index = randint(0, len(target_links) - 1)
target_links[random_index].click()

alternatively, you could look for the links that contain the partial path "colour" with the CSS selector:

a[href*='/colour/']

Upvotes: 0

undetected Selenium
undetected Selenium

Reputation: 193108

As per your question,

  • The first question Is it possible to use a stem part of multiple xpaths to create a program to randomly selects one? Answer is Yes
  • The second question Is it possible to randomly select one of the xpaths? Doesn't seems to me a valid usecase because if you select a random xpath you won't be able to Assert/validate further.

  • As per the first question a solution would be to pass the intended choice of colour to navigate and you can write a function as follows :

    def test_url(choiceColour):
        option1 = browser.find_element_by_xpath("//a[contains(@href, '" + choiceColour + "')]")
    
  • Incase you want to pass two colours of your choice you can write a function as follows :

    def test_url(firstColour, secondColour):
        option1 = browser.find_element_by_xpath("//a[contains(@href, '" + firstColour + "') and contains(@href, '" + secondColour + "')]")
    
  • Now you can call either of these functions from your main()/@Test method as per your requirement.

    Note : Incase of the function def test_url(firstColour, secondColour) passing /colour/red/yellow or /colour/yellow/red may identify the same element.

Upvotes: 0

Andersson
Andersson

Reputation: 52665

You can try to use below code:

import random

list_of_xpath = ["//a[@href='/colour/red/yellow']",
                "//a[@href='/colour/red/blue']",
                "//a[@href='/colour/blue/ornage']",
                "//a[@href='/colour/yellow/green']"]
option1 = browser.find_element_by_xpath(random.choice(list_of_xpath))

Or if you need just some random combination of colors in your XPath:

colors = ['yellow', 'blue', 'orange', 'green']
xpath = "//a[@href='/colour/{0}/{1}']".format(random.choice(colors), random.choice(colors))
option1 = browser.find_element_by_xpath(xpath)

Upvotes: 1

Gilles Quénot
Gilles Quénot

Reputation: 185219

Try this :

from random import randint

color_list = ('yellow', 'blue', 'orange', 'green')

list_len = len(color_list)
myrandint = randint(0, list_len -1)

browser.find_element_by_xpath("//a[@href='/colour/"+color_list[myrandint]+"/"+color_list[myrandint]+"']")
print(test)

Or

from random import randint

my_list = (
   "//a[@href='/colour/red/yellow']",
   "//a[@href='/colour/red/blue']",
   "//a[@href='/colour/blue/ornage']",
   "'//a[@href='/colour/yellow/green']"
)

list_len = len(color_list)
myrandint = randint(0, list_len -1)

browser.find_element_by_xpath(my_list[myrandint])

Upvotes: 1

Related Questions